! Program C:\document\alex_mad_lahey_gino\gino\zz_xwindows
! Last created on Sat May 02 20:32:46 2015
! Using GINOMENU Studio Version  v8.0
  
! Global Definitions
Module mstudio
use menu_f90
  
use Zb_physics
!use run_madx !,universe_mad=>universe, my_ring_mad=>my_ring  ,my_state_mad=>my_state
!use my_fortran
!fanglei use lin_beam
use dejan_accel
  
  
! Widget identifier definitions
integer :: Form1
integer ::  Form1Menubar
integer ::  M_control
integer ::  Form1Pop1
integer ::  m_vcontrol
integer ::  myfrotran
integer :: Form1_MainPanel
integer :: Form1_Text66
integer :: Form1_quit
integer :: Form_frame_size
integer :: Form_frame_size_MainPanel
integer :: Form_frame_size_xmin
integer :: Form_frame_size_xmax
integer :: Form_frame_size_Static1
integer :: Form_frame_size_ymin
integer :: Form_frame_size_ymax
integer :: Form_frame_size_Static2
integer :: Form_frame_size_Static3
integer :: Form_frame_size_Static4
integer :: Form_frame_size_this_plot
integer :: Form_frame_size_Text11
integer :: Form_frame_size_colour
integer :: Form_frame_size_scale
integer :: Form_frame_size_double
integer :: Form_frame_size_half
integer :: Controls
integer ::  Controls_Menubar
integer ::  mc_view
integer ::  Controls_Pop1
integer ::  mopennd
integer ::  c_norm_w
integer ::  Controls_Open_Linear_Graph_entry
integer ::  mlayoutopen
integer ::  Controls_Lattice_Function_Menu_entry
integer ::  Controls_plot_3d_open_entry
integer ::  Controls_Open_Resonance_Plot_entry
integer ::  c_plots
integer ::  Controls_Warning_entry
integer ::  Controls_Pop2
integer ::  Controls_How_and_where_to_bitch_entry
integer ::  Controls_Remove_Where
integer ::  Controls_Testing_Orbit_Lattice_entry
integer ::  Controls_Pop3
integer ::  Controls_Make_Orbit_Lattice_entry
integer ::  Controls_Quit_entry
integer ::  Controls_Pop4
integer ::  Controls_Exit_Graphic_Interface_entry
integer :: Controls_MainPanel
integer :: Controls_Button3
integer :: Controls_coord1
integer :: Controls_all
integer :: Controls_states
integer :: Controls_Button12
integer :: Controls_n_nd
integer :: Controls_MAP
integer :: Controls_mis
integer :: Controls_bump
integer :: Controls_Button18
integer :: Controls_Button20
integer :: Controls_lat_phase_tune
integer :: Controls_turns
integer :: Controls_Button23
integer :: Controls_flag_layout
integer :: Controls_Static30
integer :: Controls_layout_number
integer :: Controls_Text7
integer :: Controls_Button46
integer :: Controls_scriptfilename
integer :: Controls_Button54
integer :: Controls_Static15
integer :: Controls_Text14
integer :: Controls_Button52
integer :: Controls_flatin
integer :: Controls_flatout
integer :: Controls_Button59
integer :: Controls_track_file
integer :: Controls_Text58
integer :: Controls_Button66
integer :: Controls_Button67
integer :: Controls_Button69
integer :: Controls_Button71
integer :: Controls_ncode
integer :: Controls_Button72
integer :: Controls_kplot
integer :: Controls_Button58
integer :: Controls_Button70
integer :: Controls_Combo_script
integer :: Controls_Button80
integer :: Controls_cav_per_plot
integer :: Controls_Button85
integer :: Controls_survey_DNA
integer :: Controls_Toggle_random_colour
integer :: Controls_Button87
integer :: Controls_Buttontwiss
integer :: Controls_Text79
integer :: Controls_Button101
integer :: Controls_Button119
integer :: Controls_orbit
integer :: Controls_Panel6
integer :: Controls_angx
integer :: Controls_angy
integer :: Controls_angz
integer :: Controls_centre_layout
integer :: Controls_Button132
integer :: Controls_Button133
integer :: Controls_Button134
integer :: Controls_Button135
integer :: Controls_Button136
integer :: Controls_angle_hiromi
integer :: Controls_Text107
integer :: Controls_Text117
integer :: Controls_Button137
integer :: Controls_Button60
integer :: Controls_angle_hiromi3
integer :: Controls_angle_hiromi1
integer :: Controls_Toggle28
integer :: Controls_dotted
integer :: Controls_Button147
integer :: Controls_Button148
integer :: Controls_Button149
integer :: Controls_Button150
integer :: Controls_Button151
integer :: Controls_Text121
integer :: Controls_Text122
integer :: Controls_Text124
integer :: Controls_Button146
integer :: Controls_Button152
integer :: Controls_Button153
integer :: Controls_Button154
integer :: Controls_Button155
integer :: Controls_Button156
integer :: Controls_Button157
integer :: Controls_Text125
integer :: Controls_m_t_number
integer :: Controls_new_flat
integer :: Controls_Button139
integer :: Controls_Button140
integer :: Controls_plot_e_beam
integer :: Controls_size_e_beam
integer :: Controls_track_e
integer :: Controls_stop_lin
integer :: Controls_start_lin
integer :: Controls_n_mul
integer :: x1_x2
integer ::  x1_x2Menubar
integer ::  x1_x2_Help_entry
integer ::  x1_x2Pop1
integer ::  x1_x2_Open_Help_entry
integer ::  x1_x2_Copy_entry
integer ::  x1_x2Pop2
integer ::  x1_x2_Copy_to_Clipboard_entry
integer :: x1_x2_MainPanel
integer :: x1_x2_Graphics1
integer :: x3_x4
integer ::  x3_x4Menubar
integer ::  x3_x4_Copy_entry
integer ::  x3_x4Pop1
integer ::  x3_x4_Copy_to_Clipboard_entry
integer :: x3_x4_MainPanel
integer :: x3_x4_Graphics2
integer :: x5_x6
integer ::  x5_x6Menubar
integer ::  x5_x6_Copy_entry
integer ::  x5_x6Pop1
integer ::  x5_x6_Copy_to_Clipboard_entry
integer :: x5_x6_MainPanel
integer :: x5_x6_Graphics3
integer :: PLOT_ring
integer ::  PLOT_ring_Menubar
integer ::  m_rescale
integer ::  PLOT_ring_Pop1
integer ::  m_autos
integer ::  mu_auto
integer ::  PLOT_ring_Clear_entry
integer ::  mdraw
integer ::  PLOT_ring_Pop2
integer ::  PLOT_ring_draw_universe_entry
integer ::  PLOT_ring_Draw_Trackable_Universe_entry
integer ::  PLOT_ring_Draw_DNA_entry
integer ::  m_drawl
integer ::  m_drawmag
integer ::  PLOT_ring_Erase_and_Draw_Magnets_entry
integer ::  PLOT_ring_Graphics4_Rescale_and_Draw_entry
integer ::  m_tracking
integer ::  PLOT_ring_Pop3
integer ::  m_plot_ray
integer ::  m_scale_plot
integer ::  m_p_fix0
integer ::  m_p_fix
integer ::  M_plots
integer ::  PLOT_ring_Pop4
integer ::  PLOT_ring_Compute_Lattice_Functions_entry
integer ::  M_select
integer ::  M_plotbetax
integer ::  m_scalebetax
integer ::  M_auto_scale
integer ::  separator3
integer ::  PLOT_ring_Plot_Dipole_Component_entry
integer ::  PLOT_ring_Plot_Quadrupole_Componeny_entry
integer ::  PLOT_ring_Plot_Sextupole_Component_entry
integer ::  PLOT_ring_Plot_Soleninoid_Component_entry
integer ::  PLOT_ring_Colour_of_Dipole_Component_entry
integer ::  PLOT_ring_Colour_of_Quadrupole_Component_entry
integer ::  PLOT_ring_Colour_of_Sextupole_Component_entry
integer ::  PLOT_ring_Colour_of_Solenoid_Component_entry
integer ::  PLOT_ring_Magnet_scale_entry
integer ::  PLOT_ring_Pop5
integer ::  PLOT_ring_Scale_entry
integer ::  PLOT_ring_Scale_of_Misalignments_entry
integer ::  M_colour
integer ::  M_col_bends
integer ::  PLOT_ring_Colour_of_Solenoids_entry
integer ::  PLOT_ring_Colour_of_Cavities_entry
integer ::  PLOT_ring_Copy_entry
integer ::  PLOT_ring_Pop6
integer ::  PLOT_ring_Copy_to_Clipboard_entry
integer ::  PLOT_ring_Help_entry
integer ::  PLOT_ring_Pop7
integer ::  PLOT_ring_Help_on_holding
integer ::  PLOT_ring_Quit_entry
integer ::  PLOT_ring_Pop8
integer ::  PLOT_ring_Exit_Graphical_Interface_entry
integer :: PLOT_ring_MainPanel
integer :: PLOT_ring_Graphics4
integer :: Form_States
integer ::  Form_StatesMenubar
integer ::  Form_States_Quit_entry
integer ::  Form_StatesPop1
integer ::  Form_States_Exit_Graphical_Interface_entry
integer :: Form_States_MainPanel
integer :: Form_States_stoch
integer :: Form_States_Totalpath
integer :: Form_States_time
integer :: Form_States_radiation
integer :: Form_States_nocavity
integer :: Form_States_fringe
integer :: Form_States_exactmis
integer :: Form_States_spin
integer :: Form_States_lingyun
integer :: Form_States_only4d
integer :: Form_States_delta
integer :: Form_States_my_order
integer :: Form_States_Static19
integer :: Form_States_Button63
integer :: Form_States_Button79
integer :: Form_States_Text37
integer :: Form_States_Text38
integer :: Form_States_Text39
integer :: Form_States_Text40
integer :: Form_States_nst
integer :: Form_States_t1
integer :: Form_States_t2
integer :: Form_States_start_t
integer :: Form_States_Gauge_t
integer :: Form_States_line
integer :: Form_States_Button64
integer :: Form_States_fixpoint
integer :: Form_States_Static10
integer :: Form_States_nturn
integer :: Form_States_start
integer :: Form_States_Static11
integer :: Form_States_Text45
integer :: Form_States_Patch
integer :: Form_States_first_fib
integer :: Form_States_second_fib
integer :: Form_States_Text69
integer :: Form_States_my_delta
integer :: Form_States_Static12
integer :: Form_States_name
integer :: Form_States_fix0
integer :: Form_States_store
integer :: Form_States_aperture_check
integer :: Form_States_apx
integer :: Form_States_apy
integer :: Form_States_name_ap
integer :: Form_States_Static25
integer :: Form_States_filename_ap
integer :: Form_States_ascript
integer :: Form_States_comp_ascript
integer :: Form_States_track_map
integer :: Form_States_Text55
integer :: Form_States_Text64
integer :: Form_States_Button77
integer :: Form_States_xap
integer :: Form_States_yap
integer :: Form_States_fibre1
integer :: Form_States_Button82
integer :: Form_States_remove_ap
integer :: Form_States_gird_siam_check
integer :: Form_orbit
integer :: Form_orbit_MainPanel
integer :: Form_orbit_x
integer :: Form_orbit_px
integer :: Form_orbit_y
integer :: Form_orbit_py
integer :: Form_orbit_pt
integer :: Form_orbit_t
integer :: Form_scale
integer :: Form_scale_MainPanel
integer :: Form_scale_val
integer :: Form_mis
integer ::  Form_misMenubar
integer ::  m_mis_apply
integer ::  Form_misPop1
integer ::  m_quad_mis
integer ::  m_move_dip
integer ::  M_move_sex
integer ::  separator2
integer ::  m_all_mis
integer :: Form_mis_MainPanel
integer :: Form_mis_11
integer :: Form_mis_12
integer :: Form_mis_13
integer :: Form_mis_14
integer :: Form_mis_15
integer :: Form_mis_16
integer :: Form_mis_21
integer :: Form_mis_22
integer :: Form_mis_23
integer :: Form_mis_24
integer :: Form_mis_25
integer :: Form_mis_26
integer :: Form_mis_31
integer :: Form_mis_32
integer :: Form_mis_33
integer :: Form_mis_34
integer :: Form_mis_35
integer :: Form_mis_36
integer :: Form_mis_Static16
integer :: Form_mis_Static17
integer :: Form_mis_Static18
integer :: Form_mis_10
integer :: Form_mis_20
integer :: Form_mis_30
integer :: Form_lat
integer ::  Form_latMenubar
integer ::  lat_com
integer ::  Form_latPop1
integer ::  lat_ctc
integer ::  Form_lat_Quit_entry
integer ::  Form_latPop2
integer ::  Form_lat_Exit_Graphical_Interface_entry
integer :: Form_lat_MainPanel
integer :: Form_lat_i
integer :: Form_lat_j
integer :: Form_lat_k
integer :: Form_lat_name
integer :: Form_lat_mag_name
integer :: Form_lat_S
integer :: Form_lat_phase1
integer :: Form_lat_phase2
integer :: Form_lat_phase3
integer :: Form_lat_qx
integer :: Form_lat_qy
integer :: Form_lat_qxp
integer :: Form_lat_qyp
integer :: Form_lat_qxpp
integer :: Form_lat_qypp
integer :: Form_lat_Static22
integer :: Form_lat_Static23
integer :: Form_lat_Static24
integer :: Form_lat_d11
integer :: Form_lat_d22
integer :: Form_lat_d12
integer :: Form_lat_Static26
integer :: Form_lat_Static27
integer :: Form_lat_Static28
integer :: Form_mres1
integer :: Form_mres2
integer :: Form_mres3
integer :: Form_lat_Button42
integer :: Form_lat_Button43
integer :: Form_lat_Text65
integer :: Form_lat_Text67
integer :: Form_lat_Button84
integer :: Form_lat_res_file
integer :: Form_lat_file
integer :: Form_lat_resp
integer :: Form_lat_Button99
integer :: Form_lat_Button100
integer :: Form_lat_val
integer :: Form_lat_function_monitors
integer :: Form_dyn
integer ::  Form_dynMenubar
integer ::  a_grid
integer ::  Form_dynPop1
integer ::  a_plit_g
integer ::  M_plot_ap
integer ::  Form_dyn_Grid_Size_entry
integer ::  Form_dyn_Grid_Size_Y_entry
integer ::  Form_dyn_Use_Mouse_Interface_Dimensions_entry
integer ::  Form_dyn_Copy_entry
integer ::  Form_dynPop2
integer ::  Form_dyn_Paste_to_Clipboard_entry
integer ::  Form_dyn_Coulour_entry
integer ::  Form_dynPop3
integer ::  Form_dyn_Colour_of_Stable_Ray_entry
integer ::  Form_dyn_Colour_of_Unstable_Ray_entry
integer ::  Form_dyn_Quit_entry
integer ::  Form_dynPop4
integer ::  Form_dyn_Exit_Graphic_Interface_entry
integer :: Form_dyn_MainPanel
integer :: Form_dyn_exit
integer :: Form_dyn_stable
integer :: Form_dyn_plot
integer :: Form_dyn_Text50
integer :: Form_dyn_Text51
integer :: Form_dyn_unstable
integer :: Form_dyn_Text63
integer :: Form_dyn_Button76
integer :: Form_dyn_use_a
integer :: Form_xpx_n
integer :: Form_xpx_n_MainPanel
integer :: Form_xpx_n_G
integer :: Form_ypy_n
integer :: Form_ypy_n_MainPanel
integer :: Form_ypy_n_G
integer :: Form_explain
integer :: Form_explain_MainPanel
integer :: Form_explain_Text15
integer :: Form_explain_Text16
integer :: Form_explain_Text17
integer :: Form_explain_Text18
integer :: Form_explain_Text19
integer :: Form_explain_Text20
integer :: Form_explain_Text21
integer :: Form_explain_Text22
integer :: Form_explain_Text23
integer :: Form_explain_Text35
integer :: Form_explain_Panel4
integer :: Form_explain_Text59
integer :: Form_explain_Text61
integer :: Form_warn
integer :: Form_warn_MainPanel
integer :: Form_warn_Text24
integer :: Form_explain_layout
integer :: Form_explain_layout_MainPanel
integer :: Form_explain_layout_Text25
integer :: Form_explain_layout_Text16
integer :: Form_explain_layout_Text27
integer :: Form_explain_layout_Text28
integer :: Form_explain_layout_Text29a
integer :: Form_explain_layout_Text29b
integer :: Form_explain_layout_Text29c
integer :: Form_explain_layout_Text32
integer :: Form_explain_layout_Text33
integer :: Form_explain_layout_Text34
integer :: Form_explain_layout_Text70
integer :: Form_explain_layout_Text71
integer :: Form_explain_layout_Text72
integer :: Form_explain_layout_Text108
integer :: Form_explain_layout_Text118
integer :: Form_explain_layout_Text119
integer :: Form_help_dyn
integer :: Form_help_dyn_MainPanel
integer :: Form_help_dyn_Text47
integer :: Form_help_dyn_Text48
integer :: Form_help_dyn_Text49
integer :: Form_warn_gauge
integer :: Form_warn_gauge_MainPanel
integer :: Form_warn_gauge_Text52
integer :: Form_magnet_scale
integer :: Form_magnet_scale_MainPanel
integer :: Form_magnet_scale_scale
integer :: Form_magnet_scale_Text57
integer :: Form_magnet_scale_Text56
integer :: Form_magnet_scale_Value117
integer :: Form_magnet_scale_Text106
integer :: Plot_3D
integer :: Plot_3D_MainPanel
integer :: Plot_3D_G
integer :: Form_plot_linear
integer :: Form28_MainPanel
integer :: Form_plot_linear_Graphics
integer :: Form_plot_linear_ibpm
integer :: Form_plot_linear_nbpm
integer :: Form_plot_linear_nturn
integer :: Form_plot_linear_Text82
integer :: Form_plot_linear_Text83
integer :: Form_plot_linear_Text84
integer :: Form_plot_linear_Button102
integer :: Form_plot_linear_Button103
integer :: Form_plot_linear_Text85
integer :: Form_plot_linear_Value97
integer :: Form_plot_linear_Button104
integer :: Form_plot_linear_Button105
integer :: Form_plot_linear_Button106
integer :: Form_plot_linear_Button107
integer :: Form_plot_linear_Button108
integer :: Form_plot_linear_Value98
integer :: Form_plot_linear_Button109
integer :: Form_plot_linear_Button110
integer :: Form_plot_linear_Button111
integer :: Form_plot_linear_Button112
integer :: Form_plot_linear_Button113
integer :: Form_plot_linear_Button115
integer :: Form_plot_linear_Button116
integer :: Form_plot_linear_with_c
integer :: Form_plot_linear_all
integer :: Form_plot_linear_compute_all
integer :: Form_plot_linear_comp_lat
integer :: Form_plot_linear_Button124
integer :: Form_plot_linear_ignore
integer :: Form_plot_linear_list
integer :: Form_plot_linear_Button127
integer :: Form30_twiss
integer :: Form30_MainPanel
integer :: Form30_twiss_alx
integer :: Form30_twiss_Text75
integer :: Form30_twiss_betax
integer :: Form30_twiss_Text76
integer :: Form30_twiss_aly
integer :: Form30_twiss_Text77
integer :: Form30_twiss_betay
integer :: Form30_twiss_Text78
integer :: Form30_twiss_Button89
integer :: Form30_twiss_make_ait
integer :: Form30_twiss_qf
integer :: Form30_twiss_qd
integer :: Form30_twiss_qx
integer :: Form30_twiss_qy
integer :: Form30_twiss_Text88
integer :: Form30_twiss_Text89
integer :: Form30_twiss_Text90
integer :: Form30_twiss_Text91
integer :: Form30_twiss_fit_tunes
integer :: Form30_twiss_epsfr
integer :: Form_resonance
integer :: Form_resonance_MainPanel
integer :: Form_resonance_plot_phi_i
integer :: Form_resonance_mr1
integer :: Form_resonance_mr2
integer :: Form_resonance_dmr1
integer :: Form_resonance_dmr2
integer :: Form_resonance_dmr3
integer :: Form_resonance_a
integer :: Form_resonance_phi
integer :: Form_resonance_compute
integer :: Form_resonance_sum
integer :: Form_resonance_Text92
integer :: Form_resonance_Text93
integer :: Form_resonance_Text94
integer :: Form_resonance_Text95
integer :: Form_resonance_Text97
integer :: Form_resonance_J
integer :: Form_resonance_Text98
integer :: Form_resonance_jsol1
integer :: Form_resonance_jsol2
integer :: Form_resonance_Button129
integer :: Form_resonance_Text99
integer :: Form_resonance_dt
integer :: Form_resonance_Text100
integer :: Form_resonance_psi0
integer :: Form_resonance_i0
integer :: Form_resonance_Button130
integer :: Form_resonance_Button131
integer :: Form_resonance_Text101
integer :: Form_resonance_Text102
integer :: Form_resonance_Text103
integer :: Form_resonance_Text104
integer :: Form_resonance_Text105
integer :: callid
contains
subroutine GenerateForm1
  
! Set up master window Form1
   Form1 = gmCreateMasterWindow(24, 150, 150, 69, GALL, 'Program Alex', gmVpos=GTOP, gmInitState=GNORMAL, &
      gmIconFormat=GDLLICON, gmIconFile='MBIG1084')
  
! Create menu structure for Form1
  
! Create menu bar Form1Menubar child of Form1
   Form1Menubar = gmCreateMenuBar(Form1)
   M_control = gmCreateMenuEntry(Form1Menubar, 'Control', gmAccel=0)
  
! Create pull-down menu Form1Pop1 child of M_control
   Form1Pop1 = gmCreatePullDownMenu(M_control)
   m_vcontrol = gmCreateMenuEntry(Form1Pop1, 'View Control', gmAccel=0)
   myfrotran = gmCreateMenuEntry(Form1Pop1, 'Fit Ti8', gmAccel=0)
   call gmDefineCallback(m_vcontrol, m_vcontrol)
   call gmDefineCallback(myfrotran, myfrotran)
  
! Create main panel for form
   Form1_MainPanel=gmCreatePanel(Form1, 0, 0, 150, 69, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, gmType=GNOBOUNDARY, &
      gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create text widget Form1_Text66 child of Form1_MainPanel
   Form1_Text66 = gmCreateTextEntry(Form1_MainPanel, 0, 0, 200, 25,'Sat May 02 20:32:58 2015 ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form1_quit child of Form1_MainPanel
   Form1_quit = gmCreatePushButton(Form1_MainPanel, 40, 30, 75, 25, 'Quit', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form1_quit, (Form1_quit))
return
end subroutine
subroutine GenerateForm_frame_size
  
! Set up complex dialogue box Form_frame_size child of Form1
   Form_frame_size = gmCreateComplexDialogueBox(Form1, 190, 60, 400, 196, GALL, 'Sizes', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1005')
  
! Create main panel for form
   Form_frame_size_MainPanel=gmCreatePanel(Form_frame_size, 0, 0, 400, 196, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form_frame_size_xmin child of Form_frame_size_MainPanel
   Form_frame_size_xmin = gmCreateValueEntry(Form_frame_size_MainPanel, 20, 50, 110, 22, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_frame_size_xmin, (Form_frame_size_xmin))
  
! Create value entry Form_frame_size_xmax child of Form_frame_size_MainPanel
   Form_frame_size_xmax = gmCreateValueEntry(Form_frame_size_MainPanel, 240, 50, 110, 22, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_frame_size_xmax, (Form_frame_size_xmax))
  
! Create text widget Form_frame_size_Static1 child of Form_frame_size_MainPanel
   Form_frame_size_Static1 = gmCreateTextEntry(Form_frame_size_MainPanel, 22, 22, 88, 22,'x-min ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_frame_size_ymin child of Form_frame_size_MainPanel
   Form_frame_size_ymin = gmCreateValueEntry(Form_frame_size_MainPanel, 20, 110, 110, 22, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_frame_size_ymin, (Form_frame_size_ymin))
  
! Create value entry Form_frame_size_ymax child of Form_frame_size_MainPanel
   Form_frame_size_ymax = gmCreateValueEntry(Form_frame_size_MainPanel, 240, 110, 110, 22, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_frame_size_ymax, (Form_frame_size_ymax))
  
! Create text widget Form_frame_size_Static2 child of Form_frame_size_MainPanel
   Form_frame_size_Static2 = gmCreateTextEntry(Form_frame_size_MainPanel, 242, 22, 88, 22,'x-max ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_frame_size_Static3 child of Form_frame_size_MainPanel
   Form_frame_size_Static3 = gmCreateTextEntry(Form_frame_size_MainPanel, 22, 88, 88, 22,'y-min ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_frame_size_Static4 child of Form_frame_size_MainPanel
   Form_frame_size_Static4 = gmCreateTextEntry(Form_frame_size_MainPanel, 242, 88, 88, 22,'y-max ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_frame_size_this_plot child of Form_frame_size_MainPanel
   Form_frame_size_this_plot = gmCreateValueEntry(Form_frame_size_MainPanel, 160, 10, 44, 20, 0.000000, 2, 0, GEDIT, &
      gmVmin=0.000000, gmVmax=20.0000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_frame_size_this_plot, (Form_frame_size_this_plot))
  
! Create text widget Form_frame_size_Text11 child of Form_frame_size_MainPanel
   Form_frame_size_Text11 = gmCreateTextEntry(Form_frame_size_MainPanel, 80, 0, 56, 22,'Plane ', 10, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_frame_size_colour child of Form_frame_size_MainPanel
   Form_frame_size_colour = gmCreatePushButton(Form_frame_size_MainPanel, 150, 50, 66, 22, 'colour', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_frame_size_colour, (Form_frame_size_colour))
  
! Create button Form_frame_size_scale child of Form_frame_size_MainPanel
   Form_frame_size_scale = gmCreatePushButton(Form_frame_size_MainPanel, 110, 150, 170, 22, 'Scale with lattice functions', &
      gmVpos=GTOP, gmHelp='Compute lattice functions to use this feature', gmTabList=GON)
   call gmSetFontAttribs(Form_frame_size_scale, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_frame_size_scale, (Form_frame_size_scale))
  
! Create button Form_frame_size_double child of Form_frame_size_MainPanel
   Form_frame_size_double = gmCreatePushButton(Form_frame_size_MainPanel, 150, 80, 75, 25, 'Double Size', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_frame_size_double, (Form_frame_size_double))
  
! Create button Form_frame_size_half child of Form_frame_size_MainPanel
   Form_frame_size_half = gmCreatePushButton(Form_frame_size_MainPanel, 150, 110, 75, 22, 'Half Size', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_frame_size_half, (Form_frame_size_half))
return
end subroutine
subroutine GenerateControls
  
! Set up complex dialogue box Controls child of Form1
   Controls = gmCreateComplexDialogueBox(Form1, 330, 109, 760, 677, GALL, 'Controls', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1084')
  
! Create menu structure for Controls
  
! Create menu bar Controls_Menubar child of Controls
   Controls_Menubar = gmCreateMenuBar(Controls)
   mc_view = gmCreateMenuEntry(Controls_Menubar, 'View', gmAccel=0)
  
! Create pull-down menu Controls_Pop1 child of mc_view
   Controls_Pop1 = gmCreatePullDownMenu(mc_view)
   mopennd = gmCreateMenuEntry(Controls_Pop1, 'Open Nd Plots', gmAccel=0)
   c_norm_w = gmCreateMenuEntry(Controls_Pop1, 'Open Normalized Windows', gmAccel=0)
   Controls_Open_Linear_Graph_entry = gmCreateMenuEntry(Controls_Pop1, 'Open Linear Graph', gmAccel=0)
   mlayoutopen = gmCreateMenuEntry(Controls_Pop1, 'Open Layout Plot', gmAccel=0, gmHelp='MS Sans Serif')
   Controls_Lattice_Function_Menu_entry = gmCreateMenuEntry(Controls_Pop1, 'Lattice Functions', gmAccel=0)
   Controls_plot_3d_open_entry = gmCreateMenuEntry(Controls_Pop1, 'plot_3d_open', gmAccel=0)
   Controls_Open_Resonance_Plot_entry = gmCreateMenuEntry(Controls_Pop1, 'Open Resonance Plot', gmAccel=0)
   c_plots = gmCreateMenuEntry(Controls_Pop1, 'All Plots', gmAccel=0)
   Controls_Warning_entry = gmCreateMenuEntry(Controls_Menubar, 'Warning', gmAccel=0)
  
! Create pull-down menu Controls_Pop2 child of Controls_Warning_entry
   Controls_Pop2 = gmCreatePullDownMenu(Controls_Warning_entry)
   Controls_How_and_where_to_bitch_entry = gmCreateMenuEntry(Controls_Pop2, 'How and where to complain!', gmAccel=0)
   Controls_Remove_Where = gmCreateMenuEntry(Controls_Pop2, 'Remove Where to Complain Window permanently', gmAccel=0)
   Controls_Testing_Orbit_Lattice_entry = gmCreateMenuEntry(Controls_Menubar, 'Make Orbit Lattice', gmAccel=0)
  
! Create pull-down menu Controls_Pop3 child of Controls_Testing_Orbit_Lattice_entry
   Controls_Pop3 = gmCreatePullDownMenu(Controls_Testing_Orbit_Lattice_entry)
   Controls_Make_Orbit_Lattice_entry = gmCreateMenuEntry(Controls_Pop3, 'Make Orbit Lattice', gmAccel=0)
   Controls_Quit_entry = gmCreateMenuEntry(Controls_Menubar, 'Quit', gmAccel=0)
  
! Create pull-down menu Controls_Pop4 child of Controls_Quit_entry
   Controls_Pop4 = gmCreatePullDownMenu(Controls_Quit_entry)
   Controls_Exit_Graphic_Interface_entry = gmCreateMenuEntry(Controls_Pop4, 'Exit Graphical Interface', gmAccel=0)
   call gmDefineCallback(mopennd, mopennd)
   call gmDefineCallback(c_norm_w, c_norm_w)
   call gmDefineCallback(Controls_Open_Linear_Graph_entry, Controls_Open_Linear_Graph_entry)
   call gmDefineCallback(mlayoutopen, mlayoutopen)
   call gmDefineCallback(Controls_Lattice_Function_Menu_entry, Controls_Lattice_Function_Menu_entry)
   call gmDefineCallback(Controls_plot_3d_open_entry, Controls_plot_3d_open_entry)
   call gmDefineCallback(Controls_Open_Resonance_Plot_entry, Controls_Open_Resonance_Plot_entry)
   call gmDefineCallback(c_plots, c_plots)
   call gmDefineCallback(Controls_How_and_where_to_bitch_entry, Controls_How_and_where_to_bitch_entry)
   call gmDefineCallback(Controls_Remove_Where, Controls_Remove_Where)
   call gmDefineCallback(Controls_Make_Orbit_Lattice_entry, Controls_Make_Orbit_Lattice_entry)
   call gmDefineCallback(Controls_Exit_Graphic_Interface_entry, Controls_Exit_Graphic_Interface_entry)
  
! Create main panel for form
   Controls_MainPanel=gmCreatePanel(Controls, 0, 0, 760, 677, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create button Controls_Button3 child of Controls_MainPanel
   Controls_Button3 = gmCreatePushButton(Controls_MainPanel, 10, 0, 66, 22, 'Clear One', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button3, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineSelectCallback(Controls_Button3,(Controls_Button3+GIDENT))
  
! Create button Controls_coord1 child of Controls_MainPanel
   Controls_coord1 = gmCreatePushButton(Controls_MainPanel, 170, 0, 66, 22, 'sizes', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_coord1, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_coord1, (Controls_coord1))
  
! Create button Controls_all child of Controls_MainPanel
   Controls_all = gmCreatePushButton(Controls_MainPanel, 90, 0, 66, 22, 'Clear All', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_all, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineSelectCallback(Controls_all,(Controls_all+GIDENT))
  
! Create button Controls_states child of Controls_MainPanel
   Controls_states = gmCreatePushButton(Controls_MainPanel, 250, 0, 176, 22, 'PTC States and more', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_states, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_states, (Controls_states))
  
! Create button Controls_Button12 child of Controls_MainPanel
   Controls_Button12 = gmCreatePushButton(Controls_MainPanel, 390, 330, 66, 22, 'Closed Orbit', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button12, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button12, (Controls_Button12))
  
! Create value entry Controls_n_nd child of Controls_MainPanel
   Controls_n_nd = gmCreateValueEntry(Controls_MainPanel, 610, 470, 22, 22, 0.000000, 2, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=3.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_n_nd, (Controls_n_nd))
   call gmDefineSelectCallback(Controls_n_nd,(Controls_n_nd+GIDENT))
  
! Create button Controls_MAP child of Controls_MainPanel
   Controls_MAP = gmCreatePushButton(Controls_MainPanel, 0, 560, 66, 22, '1/3 MAP', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_MAP, (Controls_MAP))
  
! Create button Controls_mis child of Controls_MainPanel
   Controls_mis = gmCreatePushButton(Controls_MainPanel, 90, 580, 66, 20, 'Misalign', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_mis, (Controls_mis))
  
! Create button Controls_bump child of Controls_MainPanel
   Controls_bump = gmCreatePushButton(Controls_MainPanel, 0, 500, 66, 22, 'Bump off', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_bump, (Controls_bump))
  
! Create button Controls_Button18 child of Controls_MainPanel
   Controls_Button18 = gmCreatePushButton(Controls_MainPanel, 390, 380, 90, 22, 'Lattice Functions', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button18, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button18, (Controls_Button18))
  
! Create button Controls_Button20 child of Controls_MainPanel
   Controls_Button20 = gmCreatePushButton(Controls_MainPanel, 390, 510, 66, 22, 'Dyn Aper', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button20, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button20, (Controls_Button20))
  
! Create button Controls_lat_phase_tune child of Controls_MainPanel
   Controls_lat_phase_tune = gmCreatePushButton(Controls_MainPanel, 500, 320, 240, 30, 'Compute Tune and Chroms + Lattice Func'// &
     'tions', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_lat_phase_tune, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_lat_phase_tune, (Controls_lat_phase_tune))
  
! Create value entry Controls_turns child of Controls_MainPanel
   Controls_turns = gmCreateValueEntry(Controls_MainPanel, 60, 580, 22, 22, 1.00000, 2, 0, GEDIT, gmVmin=1.00000, &
      gmVmax=10.0000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_turns, (Controls_turns))
  
! Create button Controls_Button23 child of Controls_MainPanel
   Controls_Button23 = gmCreatePushButton(Controls_MainPanel, 0, 530, 60, 30, 'Map Only', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button23, (Controls_Button23))
  
! Create value entry Controls_flag_layout child of Controls_MainPanel
   Controls_flag_layout = gmCreateValueEntry(Controls_MainPanel, 166, 550, 22, 22, 0.000000, 2, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=10.0000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_flag_layout, (Controls_flag_layout))
  
! Create text widget Controls_Static30 child of Controls_MainPanel
   Controls_Static30 = gmCreateTextEntry(Controls_MainPanel, 78, 550, 66, 22,'Layout_flag ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Controls_layout_number child of Controls_MainPanel
   Controls_layout_number = gmCreateValueEntry(Controls_MainPanel, 480, 270, 32, 22, 1.00000, 2, 0, GEDIT, gmVmin=1.00000, &
      gmVmax=99.0000, gmType=GDECIMAL, gmBack1Col=2, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_layout_number, (Controls_layout_number))
  
! Create text widget Controls_Text7 child of Controls_MainPanel
   Controls_Text7 = gmCreateTextEntry(Controls_MainPanel, 410, 270, 66, 20,'m_u layout ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Text7, gmFontFace='MS Sans Serif', gmPointSize=8)
  
! Create button Controls_Button46 child of Controls_MainPanel
   Controls_Button46 = gmCreatePushButton(Controls_MainPanel, 640, 430, 66, 22, 'Survey', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button46, (Controls_Button46))
  
! Create text widget Controls_scriptfilename child of Controls_MainPanel
   Controls_scriptfilename = gmCreateTextEntry(Controls_MainPanel, 130, 310, 206, 22,' ', 255, GEDIT, gmBack1Col=0, &
      gmBack2Col=0, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_scriptfilename, (Controls_scriptfilename))
   call gmDefineFileDropCallback(Controls_scriptfilename,(Controls_scriptfilename+7*GIDENT))
  
! Create button Controls_Button54 child of Controls_MainPanel
   Controls_Button54 = gmCreatePushButton(Controls_MainPanel, 160, 580, 66, 22, 'charge/dir', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button54, (Controls_Button54))
  
! Create text widget Controls_Static15 child of Controls_MainPanel
   Controls_Static15 = gmCreateTextEntry(Controls_MainPanel, 440, 470, 150, 22,'Number of Input Windows (ND) ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Controls_Text14 child of Controls_MainPanel
   Controls_Text14 = gmCreateTextEntry(Controls_MainPanel, 0, 470, 286, 22,'Useless Gui Below do not touch ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=2 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Text14, gmFontFace='MS Sans Serif', gmPointSize=16, gmBold=1, gmUnderline=1)
  
! Create button Controls_Button52 child of Controls_MainPanel
   Controls_Button52 = gmCreatePushButton(Controls_MainPanel, 10, 280, 66, 22, 'Run Script', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button52, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button52, (Controls_Button52))
  
! Create text widget Controls_flatin child of Controls_MainPanel
   Controls_flatin = gmCreateTextEntry(Controls_MainPanel, 110, 390, 186, 22,' ', 255, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_flatin, (Controls_flatin))
   call gmDefineFileDropCallback(Controls_flatin,(Controls_flatin+7*GIDENT))
  
! Create text widget Controls_flatout child of Controls_MainPanel
   Controls_flatout = gmCreateTextEntry(Controls_MainPanel, 110, 430, 186, 22,' ', 255, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_flatout, (Controls_flatout))
   call gmDefineFileDropCallback(Controls_flatout,(Controls_flatout+7*GIDENT))
  
! Create button Controls_Button59 child of Controls_MainPanel
   Controls_Button59 = gmCreatePushButton(Controls_MainPanel, 0, 430, 88, 22, 'Print Flat file', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button59, (Controls_Button59))
  
! Create text widget Controls_track_file child of Controls_MainPanel
   Controls_track_file = gmCreateTextEntry(Controls_MainPanel, 560, 520, 176, 22,' ', 255, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_track_file, (Controls_track_file))
   call gmDefineFileDropCallback(Controls_track_file,(Controls_track_file+7*GIDENT))
  
! Create text widget Controls_Text58 child of Controls_MainPanel
   Controls_Text58 = gmCreateTextEntry(Controls_MainPanel, 470, 520, 80, 25,'Tracking Data ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Controls_Button66 child of Controls_MainPanel
   Controls_Button66 = gmCreatePushButton(Controls_MainPanel, 390, 580, 90, 22, 'Colour of Ray', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button66, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button66, (Controls_Button66))
  
! Create button Controls_Button67 child of Controls_MainPanel
   Controls_Button67 = gmCreatePushButton(Controls_MainPanel, 220, 550, 75, 25, 'print plot', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button67, (Controls_Button67))
  
! Create button Controls_Button69 child of Controls_MainPanel
   Controls_Button69 = gmCreatePushButton(Controls_MainPanel, 90, 610, 75, 25, 'Track', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button69, (Controls_Button69))
  
! Create button Controls_Button71 child of Controls_MainPanel
   Controls_Button71 = gmCreatePushButton(Controls_MainPanel, 190, 610, 75, 25, 'Stop', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button71, (Controls_Button71))
  
! Create value entry Controls_ncode child of Controls_MainPanel
   Controls_ncode = gmCreateValueEntry(Controls_MainPanel, 100, 640, 100, 25, 0.000000, 8, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=0.000000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_ncode, (Controls_ncode))
  
! Create button Controls_Button72 child of Controls_MainPanel
   Controls_Button72 = gmCreatePushButton(Controls_MainPanel, 0, 640, 75, 25, 'Movie', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button72, (Controls_Button72))
  
! Create value entry Controls_kplot child of Controls_MainPanel
   Controls_kplot = gmCreateValueEntry(Controls_MainPanel, 210, 640, 50, 25, 1.00000, 2, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=3.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_kplot, (Controls_kplot))
  
! Create button Controls_Button58 child of Controls_MainPanel
   Controls_Button58 = gmCreatePushButton(Controls_MainPanel, 0, 390, 88, 22, 'Read Flat file', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button58, (Controls_Button58))
  
! Create button Controls_Button70 child of Controls_MainPanel
   Controls_Button70 = gmCreatePushButton(Controls_MainPanel, 0, 610, 75, 25, 'prepare', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button70, (Controls_Button70))
  
! Create combo box Controls_Combo_script child of Controls_MainPanel
   Controls_Combo_script = gmCreateComboBox(Controls_MainPanel, 130, 280, 210, 100, GNONE, 0, gmSort=GUNSORTED, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Controls_Combo_script, (Controls_Combo_script))
  
! Create button Controls_Button80 child of Controls_MainPanel
   Controls_Button80 = gmCreatePushButton(Controls_MainPanel, 620, 570, 130, 22, 'Alternating Colour', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button80, (Controls_Button80))
  
! Create value entry Controls_cav_per_plot child of Controls_MainPanel
   Controls_cav_per_plot = gmCreateValueEntry(Controls_MainPanel, 270, 610, 30, 25, 1.00000, 2, 0, GEDIT, gmVmin=1.00000, &
      gmVmax=99.0000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_cav_per_plot, (Controls_cav_per_plot))
  
! Create button Controls_Button85 child of Controls_MainPanel
   Controls_Button85 = gmCreatePushButton(Controls_MainPanel, 240, 580, 75, 25, 'plot beam', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button85, (Controls_Button85))
  
! Create button Controls_survey_DNA child of Controls_MainPanel
   Controls_survey_DNA = gmCreatePushButton(Controls_MainPanel, 510, 420, 80, 30, 'Survey Trackable', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_survey_DNA, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_survey_DNA, (Controls_survey_DNA))
  
! Create toggle button Controls_Toggle_random_colour child of Controls_MainPanel
   Controls_Toggle_random_colour = gmCreateToggleButton(Controls_MainPanel, 530, 570, 70, 25, 'Random', 0, gmType=G3DCHECKBOX, &
      gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Toggle_random_colour, (Controls_Toggle_random_colour))
  
! Create button Controls_Button87 child of Controls_MainPanel
   Controls_Button87 = gmCreatePushButton(Controls_MainPanel, 500, 380, 100, 22, 'Tracked Twiss', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button87, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button87, (Controls_Button87))
  
! Create button Controls_Buttontwiss child of Controls_MainPanel
   Controls_Buttontwiss = gmCreatePushButton(Controls_MainPanel, 630, 380, 110, 25, 'Twiss/Fit', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Buttontwiss, (Controls_Buttontwiss))
  
! Create text widget Controls_Text79 child of Controls_MainPanel
   Controls_Text79 = gmCreateTextEntry(Controls_MainPanel, 530, 270, 66, 22,'m_t layout ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Text79, gmFontFace='MS Sans Serif', gmPointSize=8)
  
! Create button Controls_Button101 child of Controls_MainPanel
   Controls_Button101 = gmCreatePushButton(Controls_MainPanel, 160, 520, 30, 25, 'c_', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button101,(Controls_Button101+GIDENT))
  
! Create button Controls_Button119 child of Controls_MainPanel
   Controls_Button119 = gmCreatePushButton(Controls_MainPanel, 390, 300, 90, 20, 'Twiss window', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button119, (Controls_Button119))
  
! Create toggle button Controls_orbit child of Controls_MainPanel
   Controls_orbit = gmCreateToggleButton(Controls_MainPanel, 640, 470, 100, 22, 'Orbit tracking', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_orbit, (Controls_orbit))
  
! Create panel Controls_Panel6 child of Controls_MainPanel
   Controls_Panel6 = gmCreatePanel(Controls_MainPanel, 30, 30, 560, 240, gmTitle='Orientation Control for Layout Plot', &
      gmType=GDOUBLE, gmLineCol=138, gmFillCol=6, gmScrollMode=GNOBARS, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Panel6, gmFontFace='MS Sans Serif', gmPointSize=24)
  
! Create gauge Controls_angx child of Controls_Panel6
   Controls_angx = gmCreateGauge(Controls_Panel6, 20, 130, 220, 22, 'MSML1016', gmMinimum=0, gmMaximum=100, gmIntValue=0, &
      gmTicks1=20, gmTicks2=20, gmAccess=GEDIT, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_angx, (Controls_angx))
  
! Create gauge Controls_angy child of Controls_Panel6
   Controls_angy = gmCreateGauge(Controls_Panel6, 20, 160, 220, 22, 'MSML1012', gmMinimum=0, gmMaximum=100, gmIntValue=0, &
      gmTicks1=20, gmTicks2=20, gmAccess=GEDIT, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_angy, (Controls_angy))
  
! Create gauge Controls_angz child of Controls_Panel6
   Controls_angz = gmCreateGauge(Controls_Panel6, 20, 190, 220, 22, 'MSML1014', gmMinimum=0, gmMaximum=100, gmIntValue=0, &
      gmTicks1=20, gmTicks2=20, gmAccess=GEDIT, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_angz, (Controls_angz))
  
! Create button Controls_centre_layout child of Controls_Panel6
   Controls_centre_layout = gmCreatePushButton(Controls_Panel6, 240, 70, 90, 25, 'Clear ent and a', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_centre_layout, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineSelectCallback(Controls_centre_layout,(Controls_centre_layout+GIDENT))
  
! Create button Controls_Button132 child of Controls_Panel6
   Controls_Button132 = gmCreatePushButton(Controls_Panel6, 250, 100, 75, 25, 'Move', gmVpos=GTOP, gmHelp='Rotate View point u'// &
     'sing angles selected by red sliders', gmTabList=GON)
   call gmDefineCallback(Controls_Button132, (Controls_Button132))
  
! Create button Controls_Button133 child of Controls_Panel6
   Controls_Button133 = gmCreatePushButton(Controls_Panel6, 330, 70, 90, 25, 'Put centre in a', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button133, (Controls_Button133))
  
! Create button Controls_Button134 child of Controls_Panel6
   Controls_Button134 = gmCreatePushButton(Controls_Panel6, 250, 160, 75, 25, 'Hiromi Plot', gmVpos=GTOP, gmHelp='double click'// &
     ' for design', gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button134,(Controls_Button134+GIDENT))
   call gmDefineDoubleClickCallback(Controls_Button134,(Controls_Button134+6*GIDENT))
  
! Create button Controls_Button135 child of Controls_Panel6
   Controls_Button135 = gmCreatePushButton(Controls_Panel6, 340, 160, 75, 25, 'Plot Solenoid', gmVpos=GTOP, gmHelp='Draw Solen'// &
     'oid: red curve at 40cm', gmTabList=GON)
   call gmDefineCallback(Controls_Button135, (Controls_Button135))
  
! Create button Controls_Button136 child of Controls_Panel6
   Controls_Button136 = gmCreatePushButton(Controls_Panel6, 340, 190, 75, 25, 'Track Hiromi', gmVpos=GTOP, gmHelp='Track from '// &
     'end of selected layout', gmTabList=GON)
   call gmDefineCallback(Controls_Button136, (Controls_Button136))
  
! Create value entry Controls_angle_hiromi child of Controls_Panel6
   Controls_angle_hiromi = gmCreateValueEntry(Controls_Panel6, 210, 40, 60, 25, 0.000000, 8, 5, GEDIT, gmVmin=-1.00000, &
      gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_angle_hiromi, (Controls_angle_hiromi))
  
! Create text widget Controls_Text107 child of Controls_Panel6
   Controls_Text107 = gmCreateTextEntry(Controls_Panel6, 20, 40, 110, 25,'Initial View Angles (-> ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmHelp='Angle in tune units', &
      gmTabList=GON)
  
! Create text widget Controls_Text117 child of Controls_Panel6
   Controls_Text117 = gmCreateTextEntry(Controls_Panel6, 20, 100, 220, 25,'Angle Slider (0 to 360 degrees) ', 30000, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Text117, gmFontFace='MS Sans Serif', gmPointSize=14, gmBold=1)
  
! Create button Controls_Button137 child of Controls_Panel6
   Controls_Button137 = gmCreatePushButton(Controls_Panel6, 240, 190, 95, 25, 'Design Hiromi Plot', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Button137, (Controls_Button137))
  
! Create button Controls_Button60 child of Controls_Panel6
   Controls_Button60 = gmCreatePushButton(Controls_Panel6, 420, 20, 110, 22, 'Layout Plot', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button60, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button60, (Controls_Button60))
  
! Create value entry Controls_angle_hiromi3 child of Controls_Panel6
   Controls_angle_hiromi3 = gmCreateValueEntry(Controls_Panel6, 280, 40, 60, 25, 0.000000, 8, 5, GEDIT, gmVmin=-1.00000, &
      gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_angle_hiromi3, (Controls_angle_hiromi3))
  
! Create value entry Controls_angle_hiromi1 child of Controls_Panel6
   Controls_angle_hiromi1 = gmCreateValueEntry(Controls_Panel6, 140, 40, 60, 25, 0.000000, 8, 5, GEDIT, gmVmin=-1.00000, &
      gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_angle_hiromi1, (Controls_angle_hiromi1))
  
! Create toggle button Controls_Toggle28 child of Controls_Panel6
   Controls_Toggle28 = gmCreateToggleButton(Controls_Panel6, 10, 350, 100, 22, 'New Flat File', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_Toggle28, (Controls_Toggle28))
  
! Create toggle button Controls_dotted child of Controls_Panel6
   Controls_dotted = gmCreateToggleButton(Controls_Panel6, 430, 50, 100, 20, 'Dotted dir=-1', 0, gmType=G3DCHECKBOX, &
      gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_dotted, (Controls_dotted))
  
! Create button Controls_Button147 child of Controls_Panel6
   Controls_Button147 = gmCreatePushButton(Controls_Panel6, 460, 150, 20, 20, 'A', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button147,(Controls_Button147+GIDENT))
  
! Create button Controls_Button148 child of Controls_Panel6
   Controls_Button148 = gmCreatePushButton(Controls_Panel6, 440, 170, 20, 20, 'O', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button148,(Controls_Button148+GIDENT))
  
! Create button Controls_Button149 child of Controls_Panel6
   Controls_Button149 = gmCreatePushButton(Controls_Panel6, 440, 190, 20, 20, 'B', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button149,(Controls_Button149+GIDENT))
  
! Create button Controls_Button150 child of Controls_Panel6
   Controls_Button150 = gmCreatePushButton(Controls_Panel6, 460, 170, 20, 20, 'O', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button150,(Controls_Button150+GIDENT))
  
! Create button Controls_Button151 child of Controls_Panel6
   Controls_Button151 = gmCreatePushButton(Controls_Panel6, 460, 190, 20, 20, 'B', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button151,(Controls_Button151+GIDENT))
  
! Create text widget Controls_Text121 child of Controls_Panel6
   Controls_Text121 = gmCreateTextEntry(Controls_Panel6, 420, 120, 70, 25,'Choose Frame ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Controls_Text122 child of Controls_Panel6
   Controls_Text122 = gmCreateTextEntry(Controls_Panel6, 420, 100, 40, 25,'Fibre ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Controls_Text124 child of Controls_Panel6
   Controls_Text124 = gmCreateTextEntry(Controls_Panel6, 460, 100, 40, 25,'Magnet ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Controls_Button146 child of Controls_Panel6
   Controls_Button146 = gmCreatePushButton(Controls_Panel6, 440, 150, 20, 20, 'A', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button146,(Controls_Button146+GIDENT))
  
! Create button Controls_Button152 child of Controls_Panel6
   Controls_Button152 = gmCreatePushButton(Controls_Panel6, 520, 150, 20, 20, 'A', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button152,(Controls_Button152+GIDENT))
  
! Create button Controls_Button153 child of Controls_Panel6
   Controls_Button153 = gmCreatePushButton(Controls_Panel6, 500, 150, 20, 20, 'A', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button153,(Controls_Button153+GIDENT))
  
! Create button Controls_Button154 child of Controls_Panel6
   Controls_Button154 = gmCreatePushButton(Controls_Panel6, 500, 170, 20, 20, 'O', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button154,(Controls_Button154+GIDENT))
  
! Create button Controls_Button155 child of Controls_Panel6
   Controls_Button155 = gmCreatePushButton(Controls_Panel6, 520, 170, 20, 20, 'O', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button155,(Controls_Button155+GIDENT))
  
! Create button Controls_Button156 child of Controls_Panel6
   Controls_Button156 = gmCreatePushButton(Controls_Panel6, 500, 190, 20, 20, 'B', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button156,(Controls_Button156+GIDENT))
  
! Create button Controls_Button157 child of Controls_Panel6
   Controls_Button157 = gmCreatePushButton(Controls_Panel6, 520, 190, 20, 20, 'B', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_Button157,(Controls_Button157+GIDENT))
  
! Create text widget Controls_Text125 child of Controls_Panel6
   Controls_Text125 = gmCreateTextEntry(Controls_Panel6, 500, 120, 40, 25,'Draw ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Controls_m_t_number child of Controls_MainPanel
   Controls_m_t_number = gmCreateValueEntry(Controls_MainPanel, 600, 270, 32, 22, 0.000000, 2, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=99.0000, gmType=GDECIMAL, gmBack1Col=5, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_m_t_number, (Controls_m_t_number))
  
! Create toggle button Controls_new_flat child of Controls_MainPanel
   Controls_new_flat = gmCreateToggleButton(Controls_MainPanel, 10, 350, 100, 22, 'New Flat File', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_new_flat, (Controls_new_flat))
  
! Create button Controls_Button139 child of Controls_MainPanel
   Controls_Button139 = gmCreatePushButton(Controls_MainPanel, 390, 420, 80, 30, 'Survey Database', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button139, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Controls_Button139, (Controls_Button139))
  
! Create button Controls_Button140 child of Controls_MainPanel
   Controls_Button140 = gmCreatePushButton(Controls_MainPanel, 130, 340, 170, 30, 'Enter one command on terminal', &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Controls_Button140, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineSelectCallback(Controls_Button140,(Controls_Button140+GIDENT))
  
! Create button Controls_plot_e_beam child of Controls_MainPanel
   Controls_plot_e_beam = gmCreatePushButton(Controls_MainPanel, 620, 40, 75, 25, 'Plot e_beam', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_plot_e_beam,(Controls_plot_e_beam+GIDENT))
  
! Create button Controls_size_e_beam child of Controls_MainPanel
   Controls_size_e_beam = gmCreatePushButton(Controls_MainPanel, 630, 140, 100, 25, 'Size with e_beam', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_size_e_beam,(Controls_size_e_beam+GIDENT))
  
! Create button Controls_track_e child of Controls_MainPanel
   Controls_track_e = gmCreatePushButton(Controls_MainPanel, 620, 80, 110, 25, 'Track e_beam', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_track_e,(Controls_track_e+GIDENT))
  
! Create button Controls_stop_lin child of Controls_MainPanel
   Controls_stop_lin = gmCreatePushButton(Controls_MainPanel, 680, 180, 75, 25, 'Stop', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_stop_lin,(Controls_stop_lin+GIDENT))
  
! Create button Controls_start_lin child of Controls_MainPanel
   Controls_start_lin = gmCreatePushButton(Controls_MainPanel, 600, 180, 75, 25, 'Start', gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Controls_start_lin,(Controls_start_lin+GIDENT))
  
! Create value entry Controls_n_mul child of Controls_MainPanel
   Controls_n_mul = gmCreateValueEntry(Controls_MainPanel, 530, 610, 22, 22, 1.00000, 2, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=20.0000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Controls_n_mul, (Controls_n_mul))
   call gmDefineSelectCallback(Controls_n_mul,(Controls_n_mul+GIDENT))
return
end subroutine
subroutine Generatex1_x2
  
! Set up complex dialogue box x1_x2 child of Form1
   x1_x2 = gmCreateComplexDialogueBox(Form1, 243, 105, 148, 150, GALL, 'X-Px', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1029')
  
! Create menu structure for x1_x2
  
! Create menu bar x1_x2Menubar child of x1_x2
   x1_x2Menubar = gmCreateMenuBar(x1_x2)
   x1_x2_Help_entry = gmCreateMenuEntry(x1_x2Menubar, 'Help', gmAccel=0)
  
! Create pull-down menu x1_x2Pop1 child of x1_x2_Help_entry
   x1_x2Pop1 = gmCreatePullDownMenu(x1_x2_Help_entry)
   x1_x2_Open_Help_entry = gmCreateMenuEntry(x1_x2Pop1, 'Help on holding keys while clicking', gmAccel=0)
   x1_x2_Copy_entry = gmCreateMenuEntry(x1_x2Menubar, 'Copy', gmAccel=0)
  
! Create pull-down menu x1_x2Pop2 child of x1_x2_Copy_entry
   x1_x2Pop2 = gmCreatePullDownMenu(x1_x2_Copy_entry)
   x1_x2_Copy_to_Clipboard_entry = gmCreateMenuEntry(x1_x2Pop2, 'Copy to Clipboard', gmAccel=0)
   call gmDefineSelectCallback(x1_x2,(x1_x2+GIDENT))
   call gmDefineCallback(x1_x2_Open_Help_entry, x1_x2_Open_Help_entry)
   call gmDefineCallback(x1_x2_Copy_to_Clipboard_entry, x1_x2_Copy_to_Clipboard_entry)
  
! Create main panel for form
   x1_x2_MainPanel=gmCreatePanel(x1_x2, 0, 0, 148, 150, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, gmType=GNOBOUNDARY, &
      gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame x1_x2_Graphics1 child of x1_x2_MainPanel
   x1_x2_Graphics1 = gmCreateGraphicsFrame(x1_x2_MainPanel, 4, 4, 0, 0, &
         GLIMIT(0.000000, 1000.000000, 0.000000, 750.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON)
   call gmDefineSelectCallback(x1_x2_Graphics1,(x1_x2_Graphics1+GIDENT))
   call gmDefineMoveCallback(x1_x2_Graphics1,(x1_x2_Graphics1+2*GIDENT))
return
end subroutine
subroutine Generatex3_x4
  
! Set up complex dialogue box x3_x4 child of Form1
   x3_x4 = gmCreateComplexDialogueBox(Form1, 364, 106, 148, 150, GALL, 'Y-Py', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1084')
  
! Create menu structure for x3_x4
  
! Create menu bar x3_x4Menubar child of x3_x4
   x3_x4Menubar = gmCreateMenuBar(x3_x4)
   x3_x4_Copy_entry = gmCreateMenuEntry(x3_x4Menubar, 'Copy', gmAccel=0)
  
! Create pull-down menu x3_x4Pop1 child of x3_x4_Copy_entry
   x3_x4Pop1 = gmCreatePullDownMenu(x3_x4_Copy_entry)
   x3_x4_Copy_to_Clipboard_entry = gmCreateMenuEntry(x3_x4Pop1, 'Copy to Clipboard', gmAccel=0)
   call gmDefineSelectCallback(x3_x4,(x3_x4+GIDENT))
   call gmDefineCallback(x3_x4_Copy_to_Clipboard_entry, x3_x4_Copy_to_Clipboard_entry)
  
! Create main panel for form
   x3_x4_MainPanel=gmCreatePanel(x3_x4, 0, 0, 148, 150, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, gmType=GNOBOUNDARY, &
      gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame x3_x4_Graphics2 child of x3_x4_MainPanel
   x3_x4_Graphics2 = gmCreateGraphicsFrame(x3_x4_MainPanel, 9, 7, 0, 0, &
         GLIMIT(0.000000, 1000.000000, 0.000000, 750.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON)
   call gmDefineSelectCallback(x3_x4_Graphics2,(x3_x4_Graphics2+GIDENT))
return
end subroutine
subroutine Generatex5_x6
  
! Set up complex dialogue box x5_x6 child of Form1
   x5_x6 = gmCreateComplexDialogueBox(Form1, 615, 138, 148, 150, GALL, 'PT-T (D-L)', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1084')
  
! Create menu structure for x5_x6
  
! Create menu bar x5_x6Menubar child of x5_x6
   x5_x6Menubar = gmCreateMenuBar(x5_x6)
   x5_x6_Copy_entry = gmCreateMenuEntry(x5_x6Menubar, 'Copy', gmAccel=0)
  
! Create pull-down menu x5_x6Pop1 child of x5_x6_Copy_entry
   x5_x6Pop1 = gmCreatePullDownMenu(x5_x6_Copy_entry)
   x5_x6_Copy_to_Clipboard_entry = gmCreateMenuEntry(x5_x6Pop1, 'Copy to Clipboard', gmAccel=0)
   call gmDefineSelectCallback(x5_x6,(x5_x6+GIDENT))
   call gmDefineCallback(x5_x6_Copy_to_Clipboard_entry, x5_x6_Copy_to_Clipboard_entry)
  
! Create main panel for form
   x5_x6_MainPanel=gmCreatePanel(x5_x6, 0, 0, 148, 150, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, gmType=GNOBOUNDARY, &
      gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame x5_x6_Graphics3 child of x5_x6_MainPanel
   x5_x6_Graphics3 = gmCreateGraphicsFrame(x5_x6_MainPanel, 4, 4, 0, 0, &
         GLIMIT(0.000000, 1000.000000, 0.000000, 750.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON)
   call gmDefineSelectCallback(x5_x6_Graphics3,(x5_x6_Graphics3+GIDENT))
   call gmDefineMoveCallback(x5_x6_Graphics3,(x5_x6_Graphics3+2*GIDENT))
return
end subroutine
subroutine GeneratePLOT_ring
  
! Set up complex dialogue box PLOT_ring child of Form1
   PLOT_ring = gmCreateComplexDialogueBox(Form1, 144, 161, 620, 624, GALL, 'Layout', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1084')
  
! Create menu structure for PLOT_ring
  
! Create menu bar PLOT_ring_Menubar child of PLOT_ring
   PLOT_ring_Menubar = gmCreateMenuBar(PLOT_ring)
   m_rescale = gmCreateMenuEntry(PLOT_ring_Menubar, 'Rescale', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop1 child of m_rescale
   PLOT_ring_Pop1 = gmCreatePullDownMenu(m_rescale)
   m_autos = gmCreateMenuEntry(PLOT_ring_Pop1, 'Automatic Scales', gmAccel=0)
   mu_auto = gmCreateMenuEntry(PLOT_ring_Pop1, 'Universe Scales', gmAccel=0)
   PLOT_ring_Clear_entry = gmCreateMenuEntry(PLOT_ring_Pop1, 'Clear', gmAccel=0)
   mdraw = gmCreateMenuEntry(PLOT_ring_Menubar, 'Draw', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop2 child of mdraw
   PLOT_ring_Pop2 = gmCreatePullDownMenu(mdraw)
   PLOT_ring_draw_universe_entry = gmCreateMenuEntry(PLOT_ring_Pop2, 'Erase and Draw universe', gmAccel=0)
   PLOT_ring_Draw_Trackable_Universe_entry = gmCreateMenuEntry(PLOT_ring_Pop2, 'Draw Trackable Universe', gmAccel=0)
   PLOT_ring_Draw_DNA_entry = gmCreateMenuEntry(PLOT_ring_Pop2, 'Draw DNA', gmAccel=0)
   m_drawl = gmCreateMenuEntry(PLOT_ring_Pop2, 'Draw Layout', gmAccel=0)
   m_drawmag = gmCreateMenuEntry(PLOT_ring_Pop2, 'Draw Magnets', gmAccel=0)
   PLOT_ring_Erase_and_Draw_Magnets_entry = gmCreateMenuEntry(PLOT_ring_Pop2, 'Erase and Draw Magnets', gmAccel=0)
   PLOT_ring_Graphics4_Rescale_and_Draw_entry = gmCreateMenuEntry(PLOT_ring_Pop2, 'Rescale and Draw Magnets', gmAccel=0)
   m_tracking = gmCreateMenuEntry(PLOT_ring_Menubar, 'Tracking', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop3 child of m_tracking
   PLOT_ring_Pop3 = gmCreatePullDownMenu(m_tracking)
   m_plot_ray = gmCreateMenuEntry(PLOT_ring_Pop3, 'Plot Ray', gmAccel=0)
   m_scale_plot = gmCreateMenuEntry(PLOT_ring_Pop3, 'Scale for Plot', gmAccel=0)
   m_p_fix0 = gmCreateMenuEntry(PLOT_ring_Pop3, 'Plot Fix0', gmAccel=0)
   m_p_fix = gmCreateMenuEntry(PLOT_ring_Pop3, 'Plot Fixed Point', gmAccel=0)
   M_plots = gmCreateMenuEntry(PLOT_ring_Menubar, 'Plot', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop4 child of M_plots
   PLOT_ring_Pop4 = gmCreatePullDownMenu(M_plots)
   PLOT_ring_Compute_Lattice_Functions_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Compute Lattice Functions', gmAccel=0)
   M_select = gmCreateMenuEntry(PLOT_ring_Pop4, 'Select Lattice Function', gmAccel=0)
   M_plotbetax = gmCreateMenuEntry(PLOT_ring_Pop4, 'Plot', gmAccel=0)
   m_scalebetax = gmCreateMenuEntry(PLOT_ring_Pop4, 'Scale for Beta X', gmAccel=0)
   M_auto_scale = gmCreateMenuEntry(PLOT_ring_Pop4, 'Auto Scale', gmAccel=0)
   separator3 = gmCreateMenuSeparator(PLOT_ring_Pop4)
   PLOT_ring_Plot_Dipole_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Plot Dipole Component', gmAccel=0)
   PLOT_ring_Plot_Quadrupole_Componeny_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Plot Quadrupole Component', gmAccel=0)
   PLOT_ring_Plot_Sextupole_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Plot Sextupole Component', gmAccel=0)
   PLOT_ring_Plot_Soleninoid_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Plot Solenoid Component', gmAccel=0)
   PLOT_ring_Colour_of_Dipole_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Colour of Dipole Component', gmAccel=0)
   PLOT_ring_Colour_of_Quadrupole_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Colour of Quadrupole Component', gmAccel=0)
   PLOT_ring_Colour_of_Sextupole_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Colour of Sextupole Component', gmAccel=0)
   PLOT_ring_Colour_of_Solenoid_Component_entry = gmCreateMenuEntry(PLOT_ring_Pop4, 'Colour of Solenoid Component', gmAccel=0)
   PLOT_ring_Magnet_scale_entry = gmCreateMenuEntry(PLOT_ring_Menubar, 'Magnet scale', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop5 child of PLOT_ring_Magnet_scale_entry
   PLOT_ring_Pop5 = gmCreatePullDownMenu(PLOT_ring_Magnet_scale_entry)
   PLOT_ring_Scale_entry = gmCreateMenuEntry(PLOT_ring_Pop5, 'Scale of Magnets', gmAccel=0)
   PLOT_ring_Scale_of_Misalignments_entry = gmCreateMenuEntry(PLOT_ring_Pop5, 'Scale of Misalignments', gmAccel=0)
   M_colour = gmCreateMenuEntry(PLOT_ring_Pop5, 'Colour of Straight Magnet', gmAccel=0)
   M_col_bends = gmCreateMenuEntry(PLOT_ring_Pop5, 'Colour of Bends', gmAccel=0)
   PLOT_ring_Colour_of_Solenoids_entry = gmCreateMenuEntry(PLOT_ring_Pop5, 'Colour of Solenoids', gmAccel=0)
   PLOT_ring_Colour_of_Cavities_entry = gmCreateMenuEntry(PLOT_ring_Pop5, 'Colour of Cavities', gmAccel=0)
   PLOT_ring_Copy_entry = gmCreateMenuEntry(PLOT_ring_Menubar, 'Copy', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop6 child of PLOT_ring_Copy_entry
   PLOT_ring_Pop6 = gmCreatePullDownMenu(PLOT_ring_Copy_entry)
   PLOT_ring_Copy_to_Clipboard_entry = gmCreateMenuEntry(PLOT_ring_Pop6, 'Copy to Clipboard', gmAccel=0)
   PLOT_ring_Help_entry = gmCreateMenuEntry(PLOT_ring_Menubar, 'Help', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop7 child of PLOT_ring_Help_entry
   PLOT_ring_Pop7 = gmCreatePullDownMenu(PLOT_ring_Help_entry)
   PLOT_ring_Help_on_holding = gmCreateMenuEntry(PLOT_ring_Pop7, 'Help on holding key while clicking', gmAccel=0)
   PLOT_ring_Quit_entry = gmCreateMenuEntry(PLOT_ring_Menubar, 'Quit', gmAccel=0)
  
! Create pull-down menu PLOT_ring_Pop8 child of PLOT_ring_Quit_entry
   PLOT_ring_Pop8 = gmCreatePullDownMenu(PLOT_ring_Quit_entry)
   PLOT_ring_Exit_Graphical_Interface_entry = gmCreateMenuEntry(PLOT_ring_Pop8, 'Exit Graphical Interface', gmAccel=0)
   call gmDefineCallback(PLOT_ring, (PLOT_ring))
   call gmDefineSelectCallback(PLOT_ring,(PLOT_ring+GIDENT))
   call gmDefineCallback(m_autos, m_autos)
   call gmDefineCallback(mu_auto, mu_auto)
   call gmDefineCallback(PLOT_ring_Clear_entry, PLOT_ring_Clear_entry)
   call gmDefineCallback(PLOT_ring_draw_universe_entry, PLOT_ring_draw_universe_entry)
   call gmDefineCallback(PLOT_ring_Draw_Trackable_Universe_entry, PLOT_ring_Draw_Trackable_Universe_entry)
   call gmDefineCallback(PLOT_ring_Draw_DNA_entry, PLOT_ring_Draw_DNA_entry)
   call gmDefineCallback(m_drawl, m_drawl)
   call gmDefineCallback(m_drawmag, m_drawmag)
   call gmDefineCallback(PLOT_ring_Erase_and_Draw_Magnets_entry, PLOT_ring_Erase_and_Draw_Magnets_entry)
   call gmDefineCallback(PLOT_ring_Graphics4_Rescale_and_Draw_entry, PLOT_ring_Graphics4_Rescale_and_Draw_entry)
   call gmDefineCallback(m_plot_ray, m_plot_ray)
   call gmDefineCallback(m_scale_plot, m_scale_plot)
   call gmDefineCallback(m_p_fix0, m_p_fix0)
   call gmDefineCallback(m_p_fix, m_p_fix)
   call gmDefineCallback(PLOT_ring_Compute_Lattice_Functions_entry, PLOT_ring_Compute_Lattice_Functions_entry)
   call gmDefineCallback(M_select, M_select)
   call gmDefineCallback(M_plotbetax, M_plotbetax)
   call gmDefineCallback(m_scalebetax, m_scalebetax)
   call gmDefineCallback(M_auto_scale, M_auto_scale)
   call gmDefineCallback(PLOT_ring_Plot_Dipole_Component_entry, PLOT_ring_Plot_Dipole_Component_entry)
   call gmDefineCallback(PLOT_ring_Plot_Quadrupole_Componeny_entry, PLOT_ring_Plot_Quadrupole_Componeny_entry)
   call gmDefineCallback(PLOT_ring_Plot_Sextupole_Component_entry, PLOT_ring_Plot_Sextupole_Component_entry)
   call gmDefineCallback(PLOT_ring_Plot_Soleninoid_Component_entry, PLOT_ring_Plot_Soleninoid_Component_entry)
   call gmDefineCallback(PLOT_ring_Colour_of_Dipole_Component_entry, PLOT_ring_Colour_of_Dipole_Component_entry)
   call gmDefineCallback(PLOT_ring_Colour_of_Quadrupole_Component_entry, PLOT_ring_Colour_of_Quadrupole_Component_entry)
   call gmDefineCallback(PLOT_ring_Colour_of_Sextupole_Component_entry, PLOT_ring_Colour_of_Sextupole_Component_entry)
   call gmDefineCallback(PLOT_ring_Colour_of_Solenoid_Component_entry, PLOT_ring_Colour_of_Solenoid_Component_entry)
   call gmDefineCallback(PLOT_ring_Scale_entry, PLOT_ring_Scale_entry)
   call gmDefineCallback(PLOT_ring_Scale_of_Misalignments_entry, PLOT_ring_Scale_of_Misalignments_entry)
   call gmDefineCallback(M_colour, M_colour)
   call gmDefineCallback(M_col_bends, M_col_bends)
   call gmDefineCallback(PLOT_ring_Colour_of_Solenoids_entry, PLOT_ring_Colour_of_Solenoids_entry)
   call gmDefineCallback(PLOT_ring_Colour_of_Cavities_entry, PLOT_ring_Colour_of_Cavities_entry)
   call gmDefineCallback(PLOT_ring_Copy_to_Clipboard_entry, PLOT_ring_Copy_to_Clipboard_entry)
   call gmDefineCallback(PLOT_ring_Help_on_holding, PLOT_ring_Help_on_holding)
   call gmDefineCallback(PLOT_ring_Exit_Graphical_Interface_entry, PLOT_ring_Exit_Graphical_Interface_entry)
  
! Create main panel for form
   PLOT_ring_MainPanel=gmCreatePanel(PLOT_ring, 0, 0, 620, 624, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame PLOT_ring_Graphics4 child of PLOT_ring_MainPanel
   PLOT_ring_Graphics4 = gmCreateGraphicsFrame(PLOT_ring_MainPanel, 4, 4, 0, 0, &
         GLIMIT(0.000000, 1000.000000, 0.000000, 750.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON)
   call gmDefineCallback(PLOT_ring_Graphics4, (PLOT_ring_Graphics4))
   call gmDefineSelectCallback(PLOT_ring_Graphics4,(PLOT_ring_Graphics4+GIDENT))
   call gmDefineMoveCallback(PLOT_ring_Graphics4,(PLOT_ring_Graphics4+2*GIDENT))
return
end subroutine
subroutine GenerateForm_States
  
! Set up complex dialogue box Form_States child of Form1
   Form_States = gmCreateComplexDialogueBox(Form1, 560, 57, 529, 759, GALL, 'PTC States', gmHpos=GRIGHT, gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1061')
  
! Create menu structure for Form_States
  
! Create menu bar Form_StatesMenubar child of Form_States
   Form_StatesMenubar = gmCreateMenuBar(Form_States)
   Form_States_Quit_entry = gmCreateMenuEntry(Form_StatesMenubar, 'Quit', gmAccel=0)
  
! Create pull-down menu Form_StatesPop1 child of Form_States_Quit_entry
   Form_StatesPop1 = gmCreatePullDownMenu(Form_States_Quit_entry)
   Form_States_Exit_Graphical_Interface_entry = gmCreateMenuEntry(Form_StatesPop1, 'Exit Graphical Interface', gmAccel=0)
   call gmDefineCallback(Form_States_Exit_Graphical_Interface_entry, Form_States_Exit_Graphical_Interface_entry)
  
! Create main panel for form
   Form_States_MainPanel=gmCreatePanel(Form_States, 0, 0, 529, 759, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillCol=6, gmFillBorder=GOUTEREDGE)
  
! Create toggle button Form_States_stoch child of Form_States_MainPanel
   Form_States_stoch = gmCreateToggleButton(Form_States_MainPanel, 0, 130, 154, 22, 'Stochastic Radiation', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_stoch, (Form_States_stoch))
  
! Create toggle button Form_States_Totalpath child of Form_States_MainPanel
   Form_States_Totalpath = gmCreateToggleButton(Form_States_MainPanel, 0, 10, 154, 22, 'Use Total Time or Path', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_Totalpath, (Form_States_Totalpath))
  
! Create toggle button Form_States_time child of Form_States_MainPanel
   Form_States_time = gmCreateToggleButton(Form_States_MainPanel, 0, 30, 198, 22, 'Use Time instead of path', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_time, (Form_States_time))
  
! Create toggle button Form_States_radiation child of Form_States_MainPanel
   Form_States_radiation = gmCreateToggleButton(Form_States_MainPanel, 0, 50, 154, 22, 'Turn on Radiation', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_radiation, (Form_States_radiation))
  
! Create toggle button Form_States_nocavity child of Form_States_MainPanel
   Form_States_nocavity = gmCreateToggleButton(Form_States_MainPanel, 0, 70, 132, 22, 'No Cavity', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_nocavity, (Form_States_nocavity))
  
! Create toggle button Form_States_fringe child of Form_States_MainPanel
   Form_States_fringe = gmCreateToggleButton(Form_States_MainPanel, 0, 90, 154, 22, 'Turn on Quad Fringe', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_fringe, (Form_States_fringe))
  
! Create toggle button Form_States_exactmis child of Form_States_MainPanel
   Form_States_exactmis = gmCreateToggleButton(Form_States_MainPanel, 0, 110, 154, 22, 'Exact Misalignments', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_exactmis, (Form_States_exactmis))
  
! Create toggle button Form_States_spin child of Form_States_MainPanel
   Form_States_spin = gmCreateToggleButton(Form_States_MainPanel, 0, 150, 154, 22, 'Spin', 0, gmType=GTICKBOX, gmTextCol=7, &
      gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_spin, (Form_States_spin))
  
! Create toggle button Form_States_lingyun child of Form_States_MainPanel
   Form_States_lingyun = gmCreateToggleButton(Form_States_MainPanel, 0, 260, 100, 25, 'Lingyun''s TPSA', 0, gmType=GTICKBOX, &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_lingyun, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_States_lingyun, (Form_States_lingyun))
  
! Create toggle button Form_States_only4d child of Form_States_MainPanel
   Form_States_only4d = gmCreateToggleButton(Form_States_MainPanel, 0, 240, 66, 22, 'Only 4-d', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_only4d, (Form_States_only4d))
  
! Create toggle button Form_States_delta child of Form_States_MainPanel
   Form_States_delta = gmCreateToggleButton(Form_States_MainPanel, 0, 220, 80, 22, '4-d +delta', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_delta, (Form_States_delta))
  
! Create value entry Form_States_my_order child of Form_States_MainPanel
   Form_States_my_order = gmCreateValueEntry(Form_States_MainPanel, 130, 220, 22, 22, 11.0000, 2, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=1, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_my_order, (Form_States_my_order))
  
! Create text widget Form_States_Static19 child of Form_States_MainPanel
   Form_States_Static19 = gmCreateTextEntry(Form_States_MainPanel, 100, 220, 22, 22,'No ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_States_Button63 child of Form_States_MainPanel
   Form_States_Button63 = gmCreatePushButton(Form_States_MainPanel, 240, 10, 154, 20, 'Make Node Layout', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_Button63, (Form_States_Button63))
  
! Create button Form_States_Button79 child of Form_States_MainPanel
   Form_States_Button79 = gmCreatePushButton(Form_States_MainPanel, 240, 30, 75, 25, 'Fill reference', gmVpos=GTOP, gmHelp='Fi'// &
     'll Reference Ray for Plot with Closed Orbit', gmTabList=GON)
   call gmSetFontAttribs(Form_States_Button79, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_States_Button79, (Form_States_Button79))
  
! Create text widget Form_States_Text37 child of Form_States_MainPanel
   Form_States_Text37 = gmCreateTextEntry(Form_States_MainPanel, 360, 50, 44, 22,'nst+4 ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_States_Text38 child of Form_States_MainPanel
   Form_States_Text38 = gmCreateTextEntry(Form_States_MainPanel, 360, 70, 44, 22,'Entrance ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_States_Text39 child of Form_States_MainPanel
   Form_States_Text39 = gmCreateTextEntry(Form_States_MainPanel, 360, 90, 44, 22,'Exit ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_States_Text40 child of Form_States_MainPanel
   Form_States_Text40 = gmCreateTextEntry(Form_States_MainPanel, 260, 120, 154, 22,'Tracking Node Start Position ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_States_nst child of Form_States_MainPanel
   Form_States_nst = gmCreateValueEntry(Form_States_MainPanel, 420, 50, 44, 22, 0.000000, 4, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=28, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmSetWidgetStatus(Form_States_nst, GUNSELECTABLE)
  
! Create value entry Form_States_t1 child of Form_States_MainPanel
   Form_States_t1 = gmCreateValueEntry(Form_States_MainPanel, 420, 72, 66, 22, 0.000000, 10, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=28, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmSetWidgetStatus(Form_States_t1, GUNSELECTABLE)
  
! Create value entry Form_States_t2 child of Form_States_MainPanel
   Form_States_t2 = gmCreateValueEntry(Form_States_MainPanel, 420, 94, 66, 22, 0.000000, 10, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=28, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmSetWidgetStatus(Form_States_t2, GUNSELECTABLE)
  
! Create value entry Form_States_start_t child of Form_States_MainPanel
   Form_States_start_t = gmCreateValueEntry(Form_States_MainPanel, 420, 116, 66, 22, 0.000000, 4, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=28, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_start_t, (Form_States_start_t))
   call gmSetWidgetStatus(Form_States_start_t, GUNSELECTABLE)
  
! Create gauge Form_States_Gauge_t child of Form_States_MainPanel
   Form_States_Gauge_t = gmCreateGauge(Form_States_MainPanel, 250, 160, 220, 22, 'MSML1012', gmMinimum=0, gmMaximum=100, &
      gmIntValue=35, gmTicks1=10, gmTicks2=10, gmAccess=GEDIT, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_Gauge_t, (Form_States_Gauge_t))
  
! Create text widget Form_States_line child of Form_States_MainPanel
   Form_States_line = gmCreateTextEntry(Form_States_MainPanel, 230, 190, 240, 20,'Middle Position is ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_States_Button64 child of Form_States_MainPanel
   Form_States_Button64 = gmCreatePushButton(Form_States_MainPanel, 320, 220, 120, 30, 'Help!', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_Button64, gmFontFace='MS Sans Serif', gmPointSize=24, gmBold=GON, gmItalic=GON)
   call gmDefineCallback(Form_States_Button64, (Form_States_Button64))
  
! Create toggle button Form_States_fixpoint child of Form_States_MainPanel
   Form_States_fixpoint = gmCreateToggleButton(Form_States_MainPanel, 0, 380, 176, 22, 'Track around Closed Orbit', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_fixpoint, (Form_States_fixpoint))
  
! Create text widget Form_States_Static10 child of Form_States_MainPanel
   Form_States_Static10 = gmCreateTextEntry(Form_States_MainPanel, 20, 410, 88, 22,'# Turns ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_States_nturn child of Form_States_MainPanel
   Form_States_nturn = gmCreateValueEntry(Form_States_MainPanel, 10, 440, 88, 22, 0.000000, 9, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=4, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_nturn, (Form_States_nturn))
  
! Create value entry Form_States_start child of Form_States_MainPanel
   Form_States_start = gmCreateValueEntry(Form_States_MainPanel, 120, 440, 88, 22, 0.000000, 9, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=4, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_start, (Form_States_start))
  
! Create text widget Form_States_Static11 child of Form_States_MainPanel
   Form_States_Static11 = gmCreateTextEntry(Form_States_MainPanel, 120, 410, 88, 22,'Starting point ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_States_Text45 child of Form_States_MainPanel
   Form_States_Text45 = gmCreateTextEntry(Form_States_MainPanel, 260, 260, 242, 22,'__________________________________________'// &
     '___ ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_Text45, gmBold=1, gmUnderline=1)
  
! Create button Form_States_Patch child of Form_States_MainPanel
   Form_States_Patch = gmCreatePushButton(Form_States_MainPanel, 250, 400, 110, 20, 'Show Patch', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_Patch, (Form_States_Patch))
  
! Create value entry Form_States_first_fib child of Form_States_MainPanel
   Form_States_first_fib = gmCreateValueEntry(Form_States_MainPanel, 240, 320, 130, 22, 0.000000, 9, 3, GEDIT, &
      gmVmin=0.000000, gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=27, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_first_fib, (Form_States_first_fib))
  
! Create value entry Form_States_second_fib child of Form_States_MainPanel
   Form_States_second_fib = gmCreateValueEntry(Form_States_MainPanel, 240, 350, 130, 22, 0.000000, 10, 3, GEDIT, &
      gmVmin=0.000000, gmVmax=1.00000e+009, gmType=GDECIMAL, gmBack1Col=27, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_second_fib, (Form_States_second_fib))
  
! Create text widget Form_States_Text69 child of Form_States_MainPanel
   Form_States_Text69 = gmCreateTextEntry(Form_States_MainPanel, 240, 290, 130, 25,'Decimal Fibre Position ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_States_my_delta child of Form_States_MainPanel
   Form_States_my_delta = gmCreateValueEntry(Form_States_MainPanel, 10, 500, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-10.0000, &
      gmVmax=10.0000, gmType=GDECIMAL, gmBack1Col=4, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_my_delta, (Form_States_my_delta))
  
! Create text widget Form_States_Static12 child of Form_States_MainPanel
   Form_States_Static12 = gmCreateTextEntry(Form_States_MainPanel, 10, 470, 80, 22,'Delta ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_States_name child of Form_States_MainPanel
   Form_States_name = gmCreateTextEntry(Form_States_MainPanel, 120, 500, 158, 22,' ', 16, GEDIT, gmBack1Col=4, gmBack2Col=5, &
      gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_name, (Form_States_name))
  
! Create toggle button Form_States_fix0 child of Form_States_MainPanel
   Form_States_fix0 = gmCreateToggleButton(Form_States_MainPanel, 10, 630, 88, 20, 'Store Fix0', 0, gmType=GSIMPLECIRCLE, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_fix0, (Form_States_fix0))
  
! Create button Form_States_store child of Form_States_MainPanel
   Form_States_store = gmCreatePushButton(Form_States_MainPanel, 120, 630, 66, 22, 'Store it', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_store, (Form_States_store))
  
! Create toggle button Form_States_aperture_check child of Form_States_MainPanel
   Form_States_aperture_check = gmCreateToggleButton(Form_States_MainPanel, 340, 530, 110, 22, 'Aperture Check', 0, &
      gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_aperture_check, (Form_States_aperture_check))
  
! Create value entry Form_States_apx child of Form_States_MainPanel
   Form_States_apx = gmCreateValueEntry(Form_States_MainPanel, 150, 560, 88, 22, 0.000000, 16, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_apx, (Form_States_apx))
  
! Create value entry Form_States_apy child of Form_States_MainPanel
   Form_States_apy = gmCreateValueEntry(Form_States_MainPanel, 250, 560, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-10.0000, &
      gmVmax=10.0000, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_apy, (Form_States_apy))
  
! Create text widget Form_States_name_ap child of Form_States_MainPanel
   Form_States_name_ap = gmCreateTextEntry(Form_States_MainPanel, 20, 560, 120, 22,' ', 16, GEDIT, gmBack1Col=8, gmBack2Col=5, &
      gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_name_ap, (Form_States_name_ap))
  
! Create text widget Form_States_Static25 child of Form_States_MainPanel
   Form_States_Static25 = gmCreateTextEntry(Form_States_MainPanel, 120, 470, 88, 22,'Magnet Name ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_States_filename_ap child of Form_States_MainPanel
   Form_States_filename_ap = gmCreateTextEntry(Form_States_MainPanel, 12, 650, 242, 22,' ', 255, GEDIT, gmBack1Col=8, &
      gmBack2Col=5, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_filename_ap, (Form_States_filename_ap))
  
! Create toggle button Form_States_ascript child of Form_States_MainPanel
   Form_States_ascript = gmCreateToggleButton(Form_States_MainPanel, 12, 694, 264, 22, 'Transform with A-script  + Open/Close '// &
     'File', 0, gmType=GTICKBOX, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_ascript, (Form_States_ascript))
  
! Create button Form_States_comp_ascript child of Form_States_MainPanel
   Form_States_comp_ascript = gmCreatePushButton(Form_States_MainPanel, 12, 672, 110, 22, 'Map and A-script', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_States_comp_ascript, (Form_States_comp_ascript))
  
! Create toggle button Form_States_track_map child of Form_States_MainPanel
   Form_States_track_map = gmCreateToggleButton(Form_States_MainPanel, 12, 716, 176, 22, 'Track with Map', 0, gmType=GTICKBOX, &
      gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_track_map, (Form_States_track_map))
  
! Create text widget Form_States_Text55 child of Form_States_MainPanel
   Form_States_Text55 = gmCreateTextEntry(Form_States_MainPanel, 10, 600, 270, 22,'Not Documented Gui Below do not touch ', &
      255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=2 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_Text55, gmBold=1, gmUnderline=1)
  
! Create text widget Form_States_Text64 child of Form_States_MainPanel
   Form_States_Text64 = gmCreateTextEntry(Form_States_MainPanel, 20, 530, 100, 25,'Family Name ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_Text64, gmBold=1)
  
! Create button Form_States_Button77 child of Form_States_MainPanel
   Form_States_Button77 = gmCreatePushButton(Form_States_MainPanel, 350, 560, 75, 30, 'Make Aperture', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_Button77, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_States_Button77, (Form_States_Button77))
  
! Create text widget Form_States_xap child of Form_States_MainPanel
   Form_States_xap = gmCreateTextEntry(Form_States_MainPanel, 150, 530, 80, 25,'X-Aperture ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_xap, gmBold=1)
  
! Create text widget Form_States_yap child of Form_States_MainPanel
   Form_States_yap = gmCreateTextEntry(Form_States_MainPanel, 250, 530, 80, 25,'Y-Aperture ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_States_yap, gmBold=1)
  
! Create toggle button Form_States_fibre1 child of Form_States_MainPanel
   Form_States_fibre1 = gmCreateToggleButton(Form_States_MainPanel, 300, 650, 100, 25, 'Fibre1', 0, gmType=G3DCHECKBOX, &
      gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_States_fibre1, (Form_States_fibre1))
  
! Create button Form_States_Button82 child of Form_States_MainPanel
   Form_States_Button82 = gmCreatePushButton(Form_States_MainPanel, 290, 680, 130, 35, 'Nullify Fibre 1 and 2', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineSelectCallback(Form_States_Button82,(Form_States_Button82+GIDENT))
  
! Create button Form_States_remove_ap child of Form_States_MainPanel
   Form_States_remove_ap = gmCreatePushButton(Form_States_MainPanel, 350, 600, 75, 30, 'Remove  Apertures', gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_States_remove_ap, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_States_remove_ap, (Form_States_remove_ap))
  
! Create button Form_States_gird_siam_check child of Form_States_MainPanel
   Form_States_gird_siam_check = gmCreatePushButton(Form_States_MainPanel, 210, 730, 125, 25, 'S/G topoloy check', &
      gmVpos=GTOP, gmTabList=GON)
   call gmDefineSelectCallback(Form_States_gird_siam_check,(Form_States_gird_siam_check+GIDENT))
return
end subroutine
subroutine GenerateForm_orbit
  
! Set up complex dialogue box Form_orbit child of Form1
   Form_orbit = gmCreateComplexDialogueBox(Form1, 410, 270, 366, 111, GALL, 'Closed Orbit', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1091')
  
! Create main panel for form
   Form_orbit_MainPanel=gmCreatePanel(Form_orbit, 0, 0, 366, 111, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillCol=21, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form_orbit_x child of Form_orbit_MainPanel
   Form_orbit_x = gmCreateValueEntry(Form_orbit_MainPanel, 10, 10, 154, 22, 0.000000, 16, 8, GDISPLAY, gmType=GSCIENTIFIC, &
      gmJustify=GCENTRE, gmBack1Col=5, gmBack2Col=0, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_orbit_px child of Form_orbit_MainPanel
   Form_orbit_px = gmCreateValueEntry(Form_orbit_MainPanel, 180, 10, 154, 22, 0.000000, 16, 8, GDISPLAY, gmType=GSCIENTIFIC, &
      gmJustify=GCENTRE, gmBack1Col=5, gmBack2Col=0, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_orbit_y child of Form_orbit_MainPanel
   Form_orbit_y = gmCreateValueEntry(Form_orbit_MainPanel, 10, 40, 154, 22, 0.000000, 16, 8, GDISPLAY, gmType=GSCIENTIFIC, &
      gmJustify=GCENTRE, gmBack1Col=5, gmBack2Col=0, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_orbit_py child of Form_orbit_MainPanel
   Form_orbit_py = gmCreateValueEntry(Form_orbit_MainPanel, 180, 40, 154, 22, 0.000000, 16, 8, GDISPLAY, gmType=GSCIENTIFIC, &
      gmJustify=GCENTRE, gmBack1Col=5, gmBack2Col=0, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_orbit_pt child of Form_orbit_MainPanel
   Form_orbit_pt = gmCreateValueEntry(Form_orbit_MainPanel, 10, 70, 154, 22, 0.000000, 16, 8, GDISPLAY, gmType=GSCIENTIFIC, &
      gmJustify=GCENTRE, gmBack1Col=5, gmBack2Col=0, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_orbit_t child of Form_orbit_MainPanel
   Form_orbit_t = gmCreateValueEntry(Form_orbit_MainPanel, 180, 70, 154, 22, 0.000000, 16, 8, GDISPLAY, gmType=GSCIENTIFIC, &
      gmJustify=GCENTRE, gmBack1Col=5, gmBack2Col=0, gmTextCol=7, gmVpos=GTOP, gmTabList=GON)
return
end subroutine
subroutine GenerateForm_scale
  
! Set up complex dialogue box Form_scale child of Form1
   Form_scale = gmCreateComplexDialogueBox(Form1, 289, 299, 132, 44, GALL, 'Scale', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1084')
  
! Create main panel for form
   Form_scale_MainPanel=gmCreatePanel(Form_scale, 0, 0, 132, 44, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form_scale_val child of Form_scale_MainPanel
   Form_scale_val = gmCreateValueEntry(Form_scale_MainPanel, 10, 10, 88, 22, 0.000000, 12, 3, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+011, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_scale_val, (Form_scale_val))
return
end subroutine
subroutine GenerateForm_mis
  
! Set up complex dialogue box Form_mis child of Form1
   Form_mis = gmCreateComplexDialogueBox(Form1, 265, 294, 508, 343, GALL, 'Misalignments', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1091')
  
! Create menu structure for Form_mis
  
! Create menu bar Form_misMenubar child of Form_mis
   Form_misMenubar = gmCreateMenuBar(Form_mis)
   m_mis_apply = gmCreateMenuEntry(Form_misMenubar, 'Apply', gmAccel=0)
  
! Create pull-down menu Form_misPop1 child of m_mis_apply
   Form_misPop1 = gmCreatePullDownMenu(m_mis_apply)
   m_quad_mis = gmCreateMenuEntry(Form_misPop1, 'Move Quadrupoles', gmAccel=0)
   m_move_dip = gmCreateMenuEntry(Form_misPop1, 'Move Dipoles', gmAccel=0)
   M_move_sex = gmCreateMenuEntry(Form_misPop1, 'Move Sextupoles', gmAccel=0)
   separator2 = gmCreateMenuSeparator(Form_misPop1)
   m_all_mis = gmCreateMenuEntry(Form_misPop1, 'Move All', gmAccel=0)
   call gmDefineCallback(m_quad_mis, m_quad_mis)
   call gmDefineCallback(m_move_dip, m_move_dip)
   call gmDefineCallback(M_move_sex, M_move_sex)
   call gmDefineCallback(m_all_mis, m_all_mis)
  
! Create main panel for form
   Form_mis_MainPanel=gmCreatePanel(Form_mis, 0, 0, 508, 343, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form_mis_11 child of Form_mis_MainPanel
   Form_mis_11 = gmCreateValueEntry(Form_mis_MainPanel, 66, 88, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mis_11, (Form_mis_11))
  
! Create value entry Form_mis_12 child of Form_mis_MainPanel
   Form_mis_12 = gmCreateValueEntry(Form_mis_MainPanel, 66, 132, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_13 child of Form_mis_MainPanel
   Form_mis_13 = gmCreateValueEntry(Form_mis_MainPanel, 66, 176, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_14 child of Form_mis_MainPanel
   Form_mis_14 = gmCreateValueEntry(Form_mis_MainPanel, 66, 220, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_15 child of Form_mis_MainPanel
   Form_mis_15 = gmCreateValueEntry(Form_mis_MainPanel, 66, 264, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_16 child of Form_mis_MainPanel
   Form_mis_16 = gmCreateValueEntry(Form_mis_MainPanel, 66, 308, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_21 child of Form_mis_MainPanel
   Form_mis_21 = gmCreateValueEntry(Form_mis_MainPanel, 220, 88, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mis_21, (Form_mis_21))
  
! Create value entry Form_mis_22 child of Form_mis_MainPanel
   Form_mis_22 = gmCreateValueEntry(Form_mis_MainPanel, 220, 132, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_23 child of Form_mis_MainPanel
   Form_mis_23 = gmCreateValueEntry(Form_mis_MainPanel, 220, 176, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_24 child of Form_mis_MainPanel
   Form_mis_24 = gmCreateValueEntry(Form_mis_MainPanel, 220, 220, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_25 child of Form_mis_MainPanel
   Form_mis_25 = gmCreateValueEntry(Form_mis_MainPanel, 220, 264, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_26 child of Form_mis_MainPanel
   Form_mis_26 = gmCreateValueEntry(Form_mis_MainPanel, 220, 308, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_31 child of Form_mis_MainPanel
   Form_mis_31 = gmCreateValueEntry(Form_mis_MainPanel, 374, 88, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mis_31, (Form_mis_31))
  
! Create value entry Form_mis_32 child of Form_mis_MainPanel
   Form_mis_32 = gmCreateValueEntry(Form_mis_MainPanel, 374, 132, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_33 child of Form_mis_MainPanel
   Form_mis_33 = gmCreateValueEntry(Form_mis_MainPanel, 374, 176, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_34 child of Form_mis_MainPanel
   Form_mis_34 = gmCreateValueEntry(Form_mis_MainPanel, 374, 220, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_35 child of Form_mis_MainPanel
   Form_mis_35 = gmCreateValueEntry(Form_mis_MainPanel, 374, 264, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_36 child of Form_mis_MainPanel
   Form_mis_36 = gmCreateValueEntry(Form_mis_MainPanel, 374, 308, 110, 22, 0.000000, 8, 3, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=22, gmBack2Col=2, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_mis_Static16 child of Form_mis_MainPanel
   Form_mis_Static16 = gmCreateTextEntry(Form_mis_MainPanel, 88, 0, 88, 22,'Quadrupole ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_mis_Static17 child of Form_mis_MainPanel
   Form_mis_Static17 = gmCreateTextEntry(Form_mis_MainPanel, 242, 0, 88, 22,'Dipole ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_mis_Static18 child of Form_mis_MainPanel
   Form_mis_Static18 = gmCreateTextEntry(Form_mis_MainPanel, 374, 0, 88, 22,'Sextupole ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mis_10 child of Form_mis_MainPanel
   Form_mis_10 = gmCreateValueEntry(Form_mis_MainPanel, 66, 44, 110, 22, 0.000000, 8, 0, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=4, gmBack2Col=5, gmTextCol=1, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mis_10, (Form_mis_10))
  
! Create value entry Form_mis_20 child of Form_mis_MainPanel
   Form_mis_20 = gmCreateValueEntry(Form_mis_MainPanel, 220, 44, 110, 22, 0.000000, 8, 0, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=4, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mis_20, (Form_mis_20))
  
! Create value entry Form_mis_30 child of Form_mis_MainPanel
   Form_mis_30 = gmCreateValueEntry(Form_mis_MainPanel, 374, 44, 110, 22, 0.000000, 8, 0, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=4, gmBack2Col=5, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mis_30, (Form_mis_30))
return
end subroutine
subroutine GenerateForm_lat
  
! Set up complex dialogue box Form_lat child of Form1
   Form_lat = gmCreateComplexDialogueBox(Form1, 240, 269, 449, 473, GALL, 'Linear Lattice Functions', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1088')
  
! Create menu structure for Form_lat
  
! Create menu bar Form_latMenubar child of Form_lat
   Form_latMenubar = gmCreateMenuBar(Form_lat)
   lat_com = gmCreateMenuEntry(Form_latMenubar, 'Compute', gmAccel=0)
  
! Create pull-down menu Form_latPop1 child of lat_com
   Form_latPop1 = gmCreatePullDownMenu(lat_com)
   lat_ctc = gmCreateMenuEntry(Form_latPop1, 'Compute Tunes and Chromaticities', gmAccel=0)
   Form_lat_Quit_entry = gmCreateMenuEntry(Form_latMenubar, 'Quit', gmAccel=0)
  
! Create pull-down menu Form_latPop2 child of Form_lat_Quit_entry
   Form_latPop2 = gmCreatePullDownMenu(Form_lat_Quit_entry)
   Form_lat_Exit_Graphical_Interface_entry = gmCreateMenuEntry(Form_latPop2, 'Exit Graphical Interface', gmAccel=0)
   call gmDefineCallback(lat_ctc, lat_ctc)
   call gmDefineCallback(Form_lat_Exit_Graphical_Interface_entry, Form_lat_Exit_Graphical_Interface_entry)
  
! Create main panel for form
   Form_lat_MainPanel=gmCreatePanel(Form_lat, 0, 0, 449, 473, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form_lat_i child of Form_lat_MainPanel
   Form_lat_i = gmCreateValueEntry(Form_lat_MainPanel, 10, 40, 22, 22, 1.00000, 2, 0, GEDIT, gmVmin=0.000000, gmVmax=6.00000, &
      gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_i, (Form_lat_i))
  
! Create value entry Form_lat_j child of Form_lat_MainPanel
   Form_lat_j = gmCreateValueEntry(Form_lat_MainPanel, 30, 40, 22, 22, 1.00000, 2, 0, GEDIT, gmVmin=0.000000, gmVmax=6.00000, &
      gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_j, (Form_lat_j))
  
! Create value entry Form_lat_k child of Form_lat_MainPanel
   Form_lat_k = gmCreateValueEntry(Form_lat_MainPanel, 70, 40, 22, 22, 1.00000, 2, 0, GEDIT, gmVmin=1.00000, gmVmax=3.00000, &
      gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_k, (Form_lat_k))
  
! Create text widget Form_lat_name child of Form_lat_MainPanel
   Form_lat_name = gmCreateTextEntry(Form_lat_MainPanel, 180, 20, 198, 22,'Function Name ', 20, GDISPLAY, gmBack1Col=5, &
      gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_lat_mag_name child of Form_lat_MainPanel
   Form_lat_mag_name = gmCreateTextEntry(Form_lat_MainPanel, 190, 80, 110, 22,' ', 16, GEDIT, gmBack1Col=8, gmBack2Col=5, &
      gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_lat_S child of Form_lat_MainPanel
   Form_lat_S = gmCreateValueEntry(Form_lat_MainPanel, 120, 80, 66, 22, 0.000000, 12, 4, GDISPLAY, gmType=GDECIMAL, &
      gmBack1Col=26, gmBack2Col=0, gmTextCol=1, gmVpos=GTOP, gmTabList=GON)
   call gmSetWidgetStatus(Form_lat_S, GHIDDEN)
  
! Create value entry Form_lat_phase1 child of Form_lat_MainPanel
   Form_lat_phase1 = gmCreateValueEntry(Form_lat_MainPanel, 66, 110, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_phase2 child of Form_lat_MainPanel
   Form_lat_phase2 = gmCreateValueEntry(Form_lat_MainPanel, 176, 110, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_phase3 child of Form_lat_MainPanel
   Form_lat_phase3 = gmCreateValueEntry(Form_lat_MainPanel, 286, 110, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_qx child of Form_lat_MainPanel
   Form_lat_qx = gmCreateValueEntry(Form_lat_MainPanel, 66, 154, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_qy child of Form_lat_MainPanel
   Form_lat_qy = gmCreateValueEntry(Form_lat_MainPanel, 176, 154, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_qxp child of Form_lat_MainPanel
   Form_lat_qxp = gmCreateValueEntry(Form_lat_MainPanel, 66, 198, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-10000.0, &
      gmVmax=10000.0, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_qyp child of Form_lat_MainPanel
   Form_lat_qyp = gmCreateValueEntry(Form_lat_MainPanel, 176, 198, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1000.00, &
      gmVmax=1000.00, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_qxpp child of Form_lat_MainPanel
   Form_lat_qxpp = gmCreateValueEntry(Form_lat_MainPanel, 66, 242, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1.00000e+007, &
      gmVmax=1.00000e+007, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_qypp child of Form_lat_MainPanel
   Form_lat_qypp = gmCreateValueEntry(Form_lat_MainPanel, 176, 242, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1.00000e+007, &
      gmVmax=1.00000e+007, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create text widget Form_lat_Static22 child of Form_lat_MainPanel
   Form_lat_Static22 = gmCreateTextEntry(Form_lat_MainPanel, 22, 154, 22, 22,'Q ', 3, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_lat_Static23 child of Form_lat_MainPanel
   Form_lat_Static23 = gmCreateTextEntry(Form_lat_MainPanel, 22, 198, 22, 22,'Q'' ', 3, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_lat_Static24 child of Form_lat_MainPanel
   Form_lat_Static24 = gmCreateTextEntry(Form_lat_MainPanel, 0, 242, 44, 22,'Q"/2 ', 5, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_lat_d11 child of Form_lat_MainPanel
   Form_lat_d11 = gmCreateValueEntry(Form_lat_MainPanel, 66, 286, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1.00000e+006, &
      gmVmax=1.00000e+007, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_d22 child of Form_lat_MainPanel
   Form_lat_d22 = gmCreateValueEntry(Form_lat_MainPanel, 176, 286, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1.00000e+006, &
      gmVmax=1.00000e+007, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_lat_d12 child of Form_lat_MainPanel
   Form_lat_d12 = gmCreateValueEntry(Form_lat_MainPanel, 286, 286, 88, 22, 0.000000, 12, 8, GEDIT, gmVmin=-1.00000e+006, &
      gmVmax=1.00000e+007, gmType=GDECIMAL, gmBack1Col=8, gmBack2Col=5, gmTextCol=0, gmBorderType=GBLACK3D, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create text widget Form_lat_Static26 child of Form_lat_MainPanel
   Form_lat_Static26 = gmCreateTextEntry(Form_lat_MainPanel, 88, 264, 44, 22,'A hh ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_lat_Static27 child of Form_lat_MainPanel
   Form_lat_Static27 = gmCreateTextEntry(Form_lat_MainPanel, 198, 264, 44, 22,'A vv ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_lat_Static28 child of Form_lat_MainPanel
   Form_lat_Static28 = gmCreateTextEntry(Form_lat_MainPanel, 308, 264, 44, 22,'A hv ', 255, GDISPLAY, gmJustify=GCENTRE, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=16 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_mres1 child of Form_lat_MainPanel
   Form_mres1 = gmCreateValueEntry(Form_lat_MainPanel, 66, 330, 22, 22, 0.000000, 4, 0, GEDIT, gmVmin=-100.000, &
      gmVmax=100.000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mres1, (Form_mres1))
  
! Create value entry Form_mres2 child of Form_lat_MainPanel
   Form_mres2 = gmCreateValueEntry(Form_lat_MainPanel, 110, 330, 22, 22, 0.000000, 4, 0, GEDIT, gmVmin=-100.000, &
      gmVmax=100.000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mres2, (Form_mres2))
  
! Create value entry Form_mres3 child of Form_lat_MainPanel
   Form_mres3 = gmCreateValueEntry(Form_lat_MainPanel, 150, 330, 22, 22, 0.000000, 4, 0, GEDIT, gmVmin=-100.000, &
      gmVmax=100.000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_mres3, (Form_mres3))
  
! Create button Form_lat_Button42 child of Form_lat_MainPanel
   Form_lat_Button42 = gmCreatePushButton(Form_lat_MainPanel, 10, 370, 66, 22, 'Print Phase', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_lat_Button42, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_lat_Button42, (Form_lat_Button42))
  
! Create button Form_lat_Button43 child of Form_lat_MainPanel
   Form_lat_Button43 = gmCreatePushButton(Form_lat_MainPanel, 90, 370, 66, 22, 'Resonance', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_lat_Button43, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_lat_Button43, (Form_lat_Button43))
  
! Create text widget Form_lat_Text65 child of Form_lat_MainPanel
   Form_lat_Text65 = gmCreateTextEntry(Form_lat_MainPanel, 20, 0, 100, 20,'0 j 1 = eta_j ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_lat_Text67 child of Form_lat_MainPanel
   Form_lat_Text67 = gmCreateTextEntry(Form_lat_MainPanel, 140, 0, 100, 20,'0 0 j = phase_j ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_lat_Button84 child of Form_lat_MainPanel
   Form_lat_Button84 = gmCreatePushButton(Form_lat_MainPanel, 40, 400, 130, 25, 'Print Lattice Function', gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_lat_Button84, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_lat_Button84, (Form_lat_Button84))
  
! Create text widget Form_lat_res_file child of Form_lat_MainPanel
   Form_lat_res_file = gmCreateTextEntry(Form_lat_MainPanel, 200, 360, 186, 22,' ', 255, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_res_file, (Form_lat_res_file))
   call gmDefineFileDropCallback(Form_lat_res_file,(Form_lat_res_file+7*GIDENT))
  
! Create text widget Form_lat_file child of Form_lat_MainPanel
   Form_lat_file = gmCreateTextEntry(Form_lat_MainPanel, 200, 400, 186, 22,' ', 255, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_file, (Form_lat_file))
   call gmDefineFileDropCallback(Form_lat_file,(Form_lat_file+7*GIDENT))
  
! Create value entry Form_lat_resp child of Form_lat_MainPanel
   Form_lat_resp = gmCreateValueEntry(Form_lat_MainPanel, 190, 330, 22, 22, 0.000000, 4, 0, GEDIT, gmVmin=-100.000, &
      gmVmax=100.000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_resp, (Form_lat_resp))
  
! Create button Form_lat_Button99 child of Form_lat_MainPanel
   Form_lat_Button99 = gmCreatePushButton(Form_lat_MainPanel, 20, 430, 110, 25, 'Compute Tune', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_Button99, (Form_lat_Button99))
  
! Create button Form_lat_Button100 child of Form_lat_MainPanel
   Form_lat_Button100 = gmCreatePushButton(Form_lat_MainPanel, 140, 430, 75, 25, 'Compute A_m', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_lat_Button100, (Form_lat_Button100))
  
! Create value entry Form_lat_val child of Form_lat_MainPanel
   Form_lat_val = gmCreateValueEntry(Form_lat_MainPanel, 100, 50, 100, 25, 0.000000, 16, 8, GEDIT, gmVmin=0.000000, &
      gmVmax=0.000000, gmType=GDECIMAL, gmBack1Col=6, gmBack2Col=5, gmTextCol=1, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_lat_function_monitors child of Form_lat_MainPanel
   Form_lat_function_monitors = gmCreatePushButton(Form_lat_MainPanel, 230, 430, 190, 25, 'Print Lattice Function Monitors', &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_lat_function_monitors, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_lat_function_monitors, (Form_lat_function_monitors))
return
end subroutine
subroutine GenerateForm_dyn
  
! Set up complex dialogue box Form_dyn child of Form1
   Form_dyn = gmCreateComplexDialogueBox(Form1, 214, 201, 740, 568, GALL, 'Dynamic Aperture X-Y', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1076')
  
! Create menu structure for Form_dyn
  
! Create menu bar Form_dynMenubar child of Form_dyn
   Form_dynMenubar = gmCreateMenuBar(Form_dyn)
   a_grid = gmCreateMenuEntry(Form_dynMenubar, 'Grid', gmAccel=0)
  
! Create pull-down menu Form_dynPop1 child of a_grid
   Form_dynPop1 = gmCreatePullDownMenu(a_grid)
   a_plit_g = gmCreateMenuEntry(Form_dynPop1, 'Plot Grid', gmAccel=0)
   M_plot_ap = gmCreateMenuEntry(Form_dynPop1, 'Plot _Aperture', gmAccel=0)
   Form_dyn_Grid_Size_entry = gmCreateMenuEntry(Form_dynPop1, 'Grid Size X', gmAccel=0)
   Form_dyn_Grid_Size_Y_entry = gmCreateMenuEntry(Form_dynPop1, 'Grid Size Y', gmAccel=0)
   Form_dyn_Use_Mouse_Interface_Dimensions_entry = gmCreateMenuEntry(Form_dynPop1, 'Use Mouse Interface Dimensions', gmAccel=0)
   Form_dyn_Copy_entry = gmCreateMenuEntry(Form_dynMenubar, 'Copy Graphics', gmAccel=0)
  
! Create pull-down menu Form_dynPop2 child of Form_dyn_Copy_entry
   Form_dynPop2 = gmCreatePullDownMenu(Form_dyn_Copy_entry)
   Form_dyn_Paste_to_Clipboard_entry = gmCreateMenuEntry(Form_dynPop2, 'Copy to Clipboard', gmAccel=0)
   Form_dyn_Coulour_entry = gmCreateMenuEntry(Form_dynMenubar, 'Colour', gmAccel=0)
  
! Create pull-down menu Form_dynPop3 child of Form_dyn_Coulour_entry
   Form_dynPop3 = gmCreatePullDownMenu(Form_dyn_Coulour_entry)
   Form_dyn_Colour_of_Stable_Ray_entry = gmCreateMenuEntry(Form_dynPop3, 'Colour of Stable Ray', gmAccel=0)
   Form_dyn_Colour_of_Unstable_Ray_entry = gmCreateMenuEntry(Form_dynPop3, 'Colour of Unstable Ray', gmAccel=0)
   Form_dyn_Quit_entry = gmCreateMenuEntry(Form_dynMenubar, 'Quit', gmAccel=0)
  
! Create pull-down menu Form_dynPop4 child of Form_dyn_Quit_entry
   Form_dynPop4 = gmCreatePullDownMenu(Form_dyn_Quit_entry)
   Form_dyn_Exit_Graphic_Interface_entry = gmCreateMenuEntry(Form_dynPop4, 'Exit Graphical Interface', gmAccel=0)
   call gmDefineSelectCallback(Form_dyn,(Form_dyn+GIDENT))
   call gmDefineCallback(a_plit_g, a_plit_g)
   call gmDefineCallback(M_plot_ap, M_plot_ap)
   call gmDefineCallback(Form_dyn_Grid_Size_entry, Form_dyn_Grid_Size_entry)
   call gmDefineCallback(Form_dyn_Grid_Size_Y_entry, Form_dyn_Grid_Size_Y_entry)
   call gmDefineCallback(Form_dyn_Use_Mouse_Interface_Dimensions_entry, Form_dyn_Use_Mouse_Interface_Dimensions_entry)
   call gmDefineCallback(Form_dyn_Paste_to_Clipboard_entry, Form_dyn_Paste_to_Clipboard_entry)
   call gmDefineCallback(Form_dyn_Colour_of_Stable_Ray_entry, Form_dyn_Colour_of_Stable_Ray_entry)
   call gmDefineCallback(Form_dyn_Colour_of_Unstable_Ray_entry, Form_dyn_Colour_of_Unstable_Ray_entry)
   call gmDefineCallback(Form_dyn_Exit_Graphic_Interface_entry, Form_dyn_Exit_Graphic_Interface_entry)
  
! Create main panel for form
   Form_dyn_MainPanel=gmCreatePanel(Form_dyn, 0, 0, 740, 568, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create button Form_dyn_exit child of Form_dyn_MainPanel
   Form_dyn_exit = gmCreatePushButton(Form_dyn_MainPanel, 22, 528, 110, 22, 'Exit program', gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_dyn_exit, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_dyn_exit, (Form_dyn_exit))
  
! Create text widget Form_dyn_stable child of Form_dyn_MainPanel
   Form_dyn_stable = gmCreateTextEntry(Form_dyn_MainPanel, 462, 506, 242, 22,' ', 120, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_dyn_stable, (Form_dyn_stable))
  
! Create graphics frame Form_dyn_plot child of Form_dyn_MainPanel
   Form_dyn_plot = gmCreateGraphicsFrame(Form_dyn_MainPanel, 20, 0, 690, 440, &
         GLIMIT(-100.000000, 100.000000, -100.000000, 100.000000), gmFrameType=GGDI, gmVpos=GTOP)
   call gmDefineSelectCallback(Form_dyn_plot,(Form_dyn_plot+GIDENT))
  
! Create text widget Form_dyn_Text50 child of Form_dyn_MainPanel
   Form_dyn_Text50 = gmCreateTextEntry(Form_dyn_MainPanel, 190, 506, 272, 22, &
     'Stable Dynamic Aperture Points stored in ->>>> ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_dyn_Text51 child of Form_dyn_MainPanel
   Form_dyn_Text51 = gmCreateTextEntry(Form_dyn_MainPanel, 242, 528, 418, 22,'Change the file name and it will close the exist'// &
     'ing one and start a new one. ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_dyn_Text51, gmFontFace='MS Sans Serif', gmPointSize=8)
  
! Create text widget Form_dyn_unstable child of Form_dyn_MainPanel
   Form_dyn_unstable = gmCreateTextEntry(Form_dyn_MainPanel, 460, 480, 242, 22,' ', 120, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_dyn_unstable, (Form_dyn_unstable))
  
! Create text widget Form_dyn_Text63 child of Form_dyn_MainPanel
   Form_dyn_Text63 = gmCreateTextEntry(Form_dyn_MainPanel, 190, 480, 260, 22,'Unstable Dynamic Aperture Points stored in ->>>>'// &
     ' ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_dyn_Button76 child of Form_dyn_MainPanel
   Form_dyn_Button76 = gmCreatePushButton(Form_dyn_MainPanel, 30, 460, 100, 50, 'Use Mouse Interface Dimensions', gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_dyn_Button76, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form_dyn_Button76, (Form_dyn_Button76))
  
! Create toggle button Form_dyn_use_a child of Form_dyn_MainPanel
   Form_dyn_use_a = gmCreateToggleButton(Form_dyn_MainPanel, 180, 450, 242, 20, 'Using A to start near max(x) and max(y)', 0, &
      gmType=G3DCHECKBOX, gmVpos=GTOP, gmHelp='Initial (x,px,y,py) are determined on the basis of x,y, and A.', &
      gmHelpJustify=GLEFT, gmTabList=GON)
   call gmSetFontAttribs(Form_dyn_use_a, gmBold=GON, gmUnderline=GON)
   call gmDefineCallback(Form_dyn_use_a, (Form_dyn_use_a))
return
end subroutine
subroutine GenerateForm_xpx_n
  
! Set up complex dialogue box Form_xpx_n child of Form1
   Form_xpx_n = gmCreateComplexDialogueBox(Form1, 189, 200, 500, 500, GALL, 'x-px- normalized', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1041')
   call gmDefineSelectCallback(Form_xpx_n,(Form_xpx_n+GIDENT))
  
! Create main panel for form
   Form_xpx_n_MainPanel=gmCreatePanel(Form_xpx_n, 0, 0, 500, 500, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame Form_xpx_n_G child of Form_xpx_n_MainPanel
   Form_xpx_n_G = gmCreateGraphicsFrame(Form_xpx_n_MainPanel, 4, 4, 0, 0, &
         GLIMIT(-1000.000000, 1000.000000, -1000.000000, 1000.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, &
      gmExpand=GON)
   call gmDefineSelectCallback(Form_xpx_n_G,(Form_xpx_n_G+GIDENT))
   call gmDefineMoveCallback(Form_xpx_n_G,(Form_xpx_n_G+2*GIDENT))
return
end subroutine
subroutine GenerateForm_ypy_n
  
! Set up complex dialogue box Form_ypy_n child of Form1
   Form_ypy_n = gmCreateComplexDialogueBox(Form1, 445, 198, 500, 500, GALL, 'y-py-normalized', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1042')
   call gmDefineSelectCallback(Form_ypy_n,(Form_ypy_n+GIDENT))
  
! Create main panel for form
   Form_ypy_n_MainPanel=gmCreatePanel(Form_ypy_n, 0, 0, 500, 500, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame Form_ypy_n_G child of Form_ypy_n_MainPanel
   Form_ypy_n_G = gmCreateGraphicsFrame(Form_ypy_n_MainPanel, 4, 4, 0, 0, &
         GLIMIT(-1000.000000, 1000.000000, -1000.000000, 1000.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, &
      gmExpand=GON)
   call gmDefineSelectCallback(Form_ypy_n_G,(Form_ypy_n_G+GIDENT))
   call gmDefineMoveCallback(Form_ypy_n_G,(Form_ypy_n_G+2*GIDENT))
return
end subroutine
subroutine GenerateForm_explain
  
! Set up complex dialogue box Form_explain child of Form1
   Form_explain = gmCreateComplexDialogueBox(Form1, 253, 141, 471, 492, GALL, 'Explanations of Mouse Driven Tracking Windows', &
      gmVpos=GTOP, gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1064')
  
! Create main panel for form
   Form_explain_MainPanel=gmCreatePanel(Form_explain, 0, 0, 471, 492, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillCol=12, gmFillBorder=GOUTEREDGE)
  
! Create text widget Form_explain_Text15 child of Form_explain_MainPanel
   Form_explain_Text15 = gmCreateTextEntry(Form_explain_MainPanel, 22, 22, 440, 22,'Click left Mouse button holding (Upper or '// &
     'Lower Case): ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text15, gmFontFace='MS Sans Serif', gmPointSize=16, gmBold=1, gmUnderline=1)
  
! Create text widget Form_explain_Text16 child of Form_explain_MainPanel
   Form_explain_Text16 = gmCreateTextEntry(Form_explain_MainPanel, 22, 66, 242, 22,'B: Change background colour ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text16, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text17 child of Form_explain_MainPanel
   Form_explain_Text17 = gmCreateTextEntry(Form_explain_MainPanel, 22, 110, 242, 22,'F: Track (0,0) ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text17, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text18 child of Form_explain_MainPanel
   Form_explain_Text18 = gmCreateTextEntry(Form_explain_MainPanel, 22, 154, 242, 22,'I: Input manually ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text18, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text19 child of Form_explain_MainPanel
   Form_explain_Text19 = gmCreateTextEntry(Form_explain_MainPanel, 22, 198, 242, 22,'P: Use previous input ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text19, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text20 child of Form_explain_MainPanel
   Form_explain_Text20 = gmCreateTextEntry(Form_explain_MainPanel, 22, 242, 242, 22,'C: Continue tracking same ray ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text20, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text21 child of Form_explain_MainPanel
   Form_explain_Text21 = gmCreateTextEntry(Form_explain_MainPanel, 22, 440, 374, 22,'Click Right Mouse button to Stop Tracking'// &
     ' ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=2 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text21, gmFontFace='MS Sans Serif', gmPointSize=16, gmBold=1, gmItalic=1, gmUnderline=1)
  
! Create text widget Form_explain_Text22 child of Form_explain_MainPanel
   Form_explain_Text22 = gmCreateTextEntry(Form_explain_MainPanel, 22, 352, 242, 22,'1: Select a boundary for magnification ', &
      255, GDISPLAY, gmBack1Col=12, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text22, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text23 child of Form_explain_MainPanel
   Form_explain_Text23 = gmCreateTextEntry(Form_explain_MainPanel, 22, 374, 308, 22,'2: Select the other boundary for magnific'// &
     'ation ', 255, GDISPLAY, gmBack1Col=12, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text23, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text35 child of Form_explain_MainPanel
   Form_explain_Text35 = gmCreateTextEntry(Form_explain_MainPanel, 22, 330, 264, 22,'Rescaling with the mouse ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text35, gmFontFace='MS Sans Serif', gmPointSize=16)
  
! Create panel Form_explain_Panel4 child of Form_explain_MainPanel
   Form_explain_Panel4 = gmCreatePanel(Form_explain_MainPanel, 20, 270, 360, 60, gmType=GINVERSECHISEL, &
      gmLineBorder=GOUTEREDGE, gmFillCol=22, gmFillBorder=GOUTEREDGE, gmScrollMode=GNOBARS, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_explain_Text59 child of Form_explain_Panel4
   Form_explain_Text59 = gmCreateTextEntry(Form_explain_Panel4, 10, 10, 350, 22,'R: Reads Data from the file Tracking Data 2 N'// &
     'd columns ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text59, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_Text61 child of Form_explain_Panel4
   Form_explain_Text61 = gmCreateTextEntry(Form_explain_Panel4, 20, 30, 360, 25,'First line: number of rays and number of colu'// &
     'mns (2,4,6) ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_Text61, gmFontFace='MS Sans Serif', gmPointSize=14)
return
end subroutine
subroutine GenerateForm_warn
  
! Set up complex dialogue box Form_warn child of Form1
   Form_warn = gmCreateComplexDialogueBox(Form1, 252, 117, 652, 526, GALL, 'Where to complain!', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1100')
  
! Create main panel for form
   Form_warn_MainPanel=gmCreatePanel(Form_warn, 0, 0, 652, 526, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create text widget Form_warn_Text24 child of Form_warn_MainPanel
   Form_warn_Text24 = gmCreateTextEntry(Form_warn_MainPanel, 189, 58, 638, 594,'This is a GUI interface programmed by Etienne '// &
     'Forest. Anything that happened prior to the opening of this window is a MAD-XP issue and CERN should be contacted for bu'// &
     'gs or complaints. ||However you may contact Etienne at jceepf@hushmail.com for anything related to what happens after th'// &
     'e GUI opens. ||From now on, it is pure PTC. Do not contact CERN.||Thank you,||Etienne ||KEK|1-1 Oho|Tsukuba, Japan 305-0'// &
     '035||PS: To get rid of this warning permanently, please go to the Warning menu of the Controls window.|| ', 30000, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmHpos=GCENTRE, gmVpos=GMIDDLE, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_warn_Text24, gmFontFace='Times New Roman', gmPointSize=24)
return
end subroutine
subroutine GenerateForm_explain_layout
  
! Set up complex dialogue box Form_explain_layout child of Form1
   Form_explain_layout = gmCreateComplexDialogueBox(Form1, 320, 96, 628, 683, GALL, 'Explanation of Layout Window', &
      gmVpos=GTOP, gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1069')
  
! Create main panel for form
   Form_explain_layout_MainPanel=gmCreatePanel(Form_explain_layout, 0, 0, 628, 683, gmHpos=GCENTRE, gmVpos=GMIDDLE, &
      gmExpand=GON, gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create text widget Form_explain_layout_Text25 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text25 = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 22, 638, 22,'Click left Mouse button near'// &
     ' a magnet holding (Upper or Lower Case): ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text25, gmFontFace='MS Sans Serif', gmPointSize=16, gmBold=1, gmUnderline=1)
  
! Create text widget Form_explain_layout_Text16 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text16 = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 110, 242, 22,'L: Locate passively a magne'// &
     't on plot ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text16, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text27 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text27 = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 66, 396, 22,'All located magnets turn red'// &
     ': always watch console ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=2 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text27, gmFontFace='MS Sans Serif', gmPointSize=16)
  
! Create text widget Form_explain_layout_Text28 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text28 = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 154, 242, 22,'S: Changes the tracking pos'// &
     'ition ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text28, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text29a child of Form_explain_layout_MainPanel
   Form_explain_layout_Text29a = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 198, 506, 22,'C: Changes one multipole s'// &
     'trength of selected magnet (input on console) ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text29a, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text29b child of Form_explain_layout_MainPanel
   Form_explain_layout_Text29b = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 242, 506, 22,'A: Entrance Frame of Fibre'// &
     ' (not magnet: does not change when misaligning) ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text29b, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text29c child of Form_explain_layout_MainPanel
   Form_explain_layout_Text29c = gmCreateTextEntry(Form_explain_layout_MainPanel, 22, 286, 506, 22,'B: Exit Frame of Fibre (no'// &
     't magnet: does not change when misaligning) ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text29c, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text32 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text32 = gmCreateTextEntry(Form_explain_layout_MainPanel, 20, 530, 350, 22,'1: Select a boundary for ma'// &
     'gnification or Starting point ', 255, GDISPLAY, gmBack1Col=12, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text32, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text33 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text33 = gmCreateTextEntry(Form_explain_layout_MainPanel, 20, 550, 308, 22,'2: Select the other boundar'// &
     'y for magnification ', 255, GDISPLAY, gmBack1Col=12, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text33, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text34 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text34 = gmCreateTextEntry(Form_explain_layout_MainPanel, 20, 500, 286, 22,'Rescaling with the mouse ', &
      255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text34, gmFontFace='MS Sans Serif', gmPointSize=16)
  
! Create text widget Form_explain_layout_Text70 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text70 = gmCreateTextEntry(Form_explain_layout_MainPanel, 30, 320, 506, 22,'G: Girders flash in red/blu'// &
     'e. Use Right Button. Click to stop flashing ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text70, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text71 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text71 = gmCreateTextEntry(Form_explain_layout_MainPanel, 30, 350, 506, 22,'G: Siamese flash in green/y'// &
     'ellow. Use Left Button. Click to stop flashing ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 , &
     gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text71, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text72 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text72 = gmCreateTextEntry(Form_explain_layout_MainPanel, 30, 390, 506, 22,'Z: Fibre Tracking Locates f'// &
     'ibre 1 or 2 (See PTC States) ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text72, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text108 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text108 = gmCreateTextEntry(Form_explain_layout_MainPanel, 20, 570, 290, 22,'3: Translate to that point'// &
     ' from 1                 or ------> ', 255, GDISPLAY, gmBack1Col=12, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text108, gmFontFace='MS Sans Serif', gmPointSize=14)
  
! Create text widget Form_explain_layout_Text118 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text118 = gmCreateTextEntry(Form_explain_layout_MainPanel, 320, 570, 250, 25,'Alternatively Hold the sh'// &
     'ift key to translate ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text118, gmFontFace='MS Sans Serif', gmPointSize=12, gmBold=1)
  
! Create text widget Form_explain_layout_Text119 child of Form_explain_layout_MainPanel
   Form_explain_layout_Text119 = gmCreateTextEntry(Form_explain_layout_MainPanel, 30, 430, 350, 25,'H: Misalignments siamese  '// &
     '(right button for girders) ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, &
      gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_explain_layout_Text119, gmFontFace='MS Sans Serif', gmPointSize=14)
return
end subroutine
subroutine GenerateForm_help_dyn
  
! Set up complex dialogue box Form_help_dyn child of Form1
   Form_help_dyn = gmCreateComplexDialogueBox(Form1, 259, 116, 602, 637, GQUIT, 'Help for Dynamic Aperture', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1069')
  
! Create main panel for form
   Form_help_dyn_MainPanel=gmCreatePanel(Form_help_dyn, 0, 0, 602, 637, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillCol=29, gmFillBorder=GOUTEREDGE)
  
! Create text widget Form_help_dyn_Text47 child of Form_help_dyn_MainPanel
   Form_help_dyn_Text47 = gmCreateTextEntry(Form_help_dyn_MainPanel, 22, 44, 572, 352,'1) If you create a node_layout, you can'// &
     ' track at each integration step of PTC||2) First Select a fibre the usual ways: select the starting point by number or n'// &
     'ame. Or by going to the plot of the ring and using the mouse. (Look at the Help menu over there)||3) Then you use the Sl'// &
     'ide Gauge: |   a) the first position is the beginning of PTC''s fibre; |   b) the last position is at beginning of the l'// &
     'ast patch|   c) Notice that the last position on the slide is the same as the penultimate position: this makes the centr'// &
     'e of the slide the centre of the magnet if you use nst=even.||4) The number of nodes NST+4, the entrance of the fibre, t'// &
     'he exit and the point of tracking in the node_layout are displayed. (A fibre has an entrance patch, an entrance fringe, '// &
     'a magnet of NST steps, an exit fringe and an exit patch, i.e., NST+4 integration nodes)||5) Slide the gauge and see "Tra'// &
     'cking Node Start Position" change.||NB: Magnets with odd NST cannot be tracked in the middle. You must recut them. If au'// &
     'tomatic recut was used using a PTC command file, use EVEN THIN LENS rather than THIN LENS.||| ', 30000, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_help_dyn_Text47, gmFontFace='Times New Roman', gmPointSize=24)
  
! Create text widget Form_help_dyn_Text48 child of Form_help_dyn_MainPanel
   Form_help_dyn_Text48 = gmCreateTextEntry(Form_help_dyn_MainPanel, 22, 22, 528, 22,'Moving Through a Magnet using the Node L'// &
     'ayout of PTC ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form_help_dyn_Text48, gmFontFace='MS Sans Serif', gmPointSize=16)
  
! Create text widget Form_help_dyn_Text49 child of Form_help_dyn_MainPanel
   Form_help_dyn_Text49 = gmCreateTextEntry(Form_help_dyn_MainPanel, 22, 418, 550, 198,'You can also change the style of the i'// &
     'nitial conditions.||1) If the check box "Using A to start near max(x) and max(y)" is unchecked, then PTC uses (x,0,y,0,d'// &
     'elta,0) in the dynamic aperture search. This can be annoying since, in the absence of coupling, it explores paradoxicall'// &
     'y the limits in x'' and y''. ||2) If the box is checked, then PTC computes the coupled linear transformation M=ARA^(-1) '// &
     'and uses A to create an initial condition such that x and y are near Max(x) and Max(y) on  the trajectory. For a linear '// &
     'system they are exactly the maximum excursions of x and y. ||3)The calculations are at the fixed delta selected by the u'// &
     'ser. ||4) During an aperture scan, do not change these settings unless you change the colours used in the dynamic apertu'// &
     're plot. If you save the results in separate files, if you can of course change these settings.| ', 30000, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_help_dyn_Text49, gmFontFace='Times New Roman', gmPointSize=24)
return
end subroutine
subroutine GenerateForm_warn_gauge
  
! Set up complex dialogue box Form_warn_gauge child of Form1
   Form_warn_gauge = gmCreateComplexDialogueBox(Form1, 248, 204, 1149, 547, GALL, 'Warning', gmVpos=GTOP, gmQuitMode=GHIDE, &
      gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1099')
  
! Create main panel for form
   Form_warn_gauge_MainPanel=gmCreatePanel(Form_warn_gauge, 0, 0, 1149, 547, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillCol=2, gmFillBorder=GOUTEREDGE)
  
! Create text widget Form_warn_gauge_Text52 child of Form_warn_gauge_MainPanel
   Form_warn_gauge_Text52 = gmCreateTextEntry(Form_warn_gauge_MainPanel, 22, 22, 748, 440,'Make a Node Layout First ', 30000, &
      GDISPLAY, gmBack1Col=7, gmBack2Col=12, gmTextCol=3 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmSetFontAttribs(Form_warn_gauge_Text52, gmFontFace='MS Sans Serif', gmPointSize=24, gmBold=1)
return
end subroutine
subroutine GenerateForm_magnet_scale
  
! Set up complex dialogue box Form_magnet_scale child of Form1
   Form_magnet_scale = gmCreateComplexDialogueBox(Form1, 202, 175, 260, 174, GALL, 'Scale of Magnets', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='MBIG1036')
  
! Create main panel for form
   Form_magnet_scale_MainPanel=gmCreatePanel(Form_magnet_scale, 0, 0, 260, 174, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form_magnet_scale_scale child of Form_magnet_scale_MainPanel
   Form_magnet_scale_scale = gmCreateValueEntry(Form_magnet_scale_MainPanel, 30, 50, 88, 22, 0.000000, 12, 8, GEDIT, &
      gmVmin=0.000000, gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_magnet_scale_scale, (Form_magnet_scale_scale))
  
! Create text widget Form_magnet_scale_Text57 child of Form_magnet_scale_MainPanel
   Form_magnet_scale_Text57 = gmCreateTextEntry(Form_magnet_scale_MainPanel, 22, 22, 220, 22,'magnet compared to the window si'// &
     'ze ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_magnet_scale_Text56 child of Form_magnet_scale_MainPanel
   Form_magnet_scale_Text56 = gmCreateTextEntry(Form_magnet_scale_MainPanel, 22, 0, 220, 22,'Enter a number representing the r'// &
     'atio of the ', 255, GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=7 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create value entry Form_magnet_scale_Value117 child of Form_magnet_scale_MainPanel
   Form_magnet_scale_Value117 = gmCreateValueEntry(Form_magnet_scale_MainPanel, 30, 120, 88, 22, 0.000000, 12, 8, GEDIT, &
      gmVmin=0.000000, gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_magnet_scale_Value117, (Form_magnet_scale_Value117))
  
! Create text widget Form_magnet_scale_Text106 child of Form_magnet_scale_MainPanel
   Form_magnet_scale_Text106 = gmCreateTextEntry(Form_magnet_scale_MainPanel, 40, 90, 100, 25,'Frame scale ', 255, GDISPLAY, &
      gmBack1Col=0, gmBack2Col=12, gmTextCol=2 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
return
end subroutine
subroutine GeneratePlot_3D
  
! Set up complex dialogue box Plot_3D child of Form1
   Plot_3D = gmCreateComplexDialogueBox(Form1, 179, 187, 1114, 547, GALL, 'Plot_3D', gmHpos=GCENTRE, gmVpos=GMIDDLE, &
      gmQuitMode=GHIDE, gmInitState=GCENTRED, gmIconFormat=GDLLICON, gmIconFile='Gee')
   call gmDefineSelectCallback(Plot_3D,(Plot_3D+GIDENT))
  
! Create main panel for form
   Plot_3D_MainPanel=gmCreatePanel(Plot_3D, 0, 0, 1114, 547, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, gmType=GNOBOUNDARY, &
      gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame Plot_3D_G child of Plot_3D_MainPanel
   Plot_3D_G = gmCreateGraphicsFrame(Plot_3D_MainPanel, 4, 4, 0, 0, &
         GLIMIT(-10.000000, 10.000000, -10.000000, 10.000000), gmFrameType=GGDI, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON)
return
end subroutine
subroutine GenerateForm_plot_linear
  
! Set up complex dialogue box Form_plot_linear child of Form1
   Form_plot_linear = gmCreateComplexDialogueBox(Form1, 232, 273, 1138, 434, GALL, 'Plot Linear', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='Gee')
   call gmDefineSelectCallback(Form_plot_linear,(Form_plot_linear+GIDENT))
  
! Create main panel for form
   Form28_MainPanel=gmCreatePanel(Form_plot_linear, 0, 0, 1138, 434, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame Form_plot_linear_Graphics child of Form28_MainPanel
   Form_plot_linear_Graphics = gmCreateGraphicsFrame(Form28_MainPanel, 10, 10, 910, 320, &
         GLIMIT(-1000.000000, 1000.000000, -1000.000000, 1000.000000), gmFrameType=GGDI, gmVpos=GTOP)
   call gmDefineSelectCallback(Form_plot_linear_Graphics,(Form_plot_linear_Graphics+GIDENT))
  
! Create value entry Form_plot_linear_ibpm child of Form28_MainPanel
   Form_plot_linear_ibpm = gmCreateValueEntry(Form28_MainPanel, 90, 340, 110, 25, 0.000000, 32, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000e+006, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_ibpm, (Form_plot_linear_ibpm))
  
! Create text widget Form_plot_linear_nbpm child of Form28_MainPanel
   Form_plot_linear_nbpm = gmCreateTextEntry(Form28_MainPanel, 270, 340, 130, 25,' ', 255, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_plot_linear_nturn child of Form28_MainPanel
   Form_plot_linear_nturn = gmCreateValueEntry(Form28_MainPanel, 470, 340, 110, 25, 0.000000, 32, 0, GEDIT, gmVmin=0.000000, &
      gmVmax=10000.0, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_nturn, (Form_plot_linear_nturn))
  
! Create text widget Form_plot_linear_Text82 child of Form28_MainPanel
   Form_plot_linear_Text82 = gmCreateTextEntry(Form28_MainPanel, 50, 340, 40, 25,'BPM # ', 5, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_plot_linear_Text83 child of Form28_MainPanel
   Form_plot_linear_Text83 = gmCreateTextEntry(Form28_MainPanel, 220, 340, 30, 25,'Name ', 4, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_plot_linear_Text84 child of Form28_MainPanel
   Form_plot_linear_Text84 = gmCreateTextEntry(Form28_MainPanel, 420, 340, 50, 20,'turn # ', 6, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_plot_linear_Button102 child of Form28_MainPanel
   Form_plot_linear_Button102 = gmCreatePushButton(Form28_MainPanel, 930, 60, 75, 25, 'Plot Data', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button102, (Form_plot_linear_Button102))
   call gmDefineSelectCallback(Form_plot_linear_Button102,(Form_plot_linear_Button102+GIDENT))
  
! Create button Form_plot_linear_Button103 child of Form28_MainPanel
   Form_plot_linear_Button103 = gmCreatePushButton(Form28_MainPanel, 930, 30, 75, 25, 'Set size', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button103, (Form_plot_linear_Button103))
  
! Create text widget Form_plot_linear_Text85 child of Form28_MainPanel
   Form_plot_linear_Text85 = gmCreateTextEntry(Form28_MainPanel, 590, 340, 30, 25,'Plane ', 5, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_plot_linear_Value97 child of Form28_MainPanel
   Form_plot_linear_Value97 = gmCreateValueEntry(Form28_MainPanel, 640, 340, 30, 25, 1.00000, 2, 0, GEDIT, gmVmin=1.00000, &
      gmVmax=2.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Value97, (Form_plot_linear_Value97))
  
! Create button Form_plot_linear_Button104 child of Form28_MainPanel
   Form_plot_linear_Button104 = gmCreatePushButton(Form28_MainPanel, 930, 130, 75, 25, 'Average', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button104, (Form_plot_linear_Button104))
  
! Create button Form_plot_linear_Button105 child of Form28_MainPanel
   Form_plot_linear_Button105 = gmCreatePushButton(Form28_MainPanel, 930, 100, 80, 25, 'Clear Plot', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button105, (Form_plot_linear_Button105))
  
! Create button Form_plot_linear_Button106 child of Form28_MainPanel
   Form_plot_linear_Button106 = gmCreatePushButton(Form28_MainPanel, 10, 370, 75, 25, 'Horizontal', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button106, (Form_plot_linear_Button106))
  
! Create button Form_plot_linear_Button107 child of Form28_MainPanel
   Form_plot_linear_Button107 = gmCreatePushButton(Form28_MainPanel, 100, 370, 75, 25, 'Vertical', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button107, (Form_plot_linear_Button107))
  
! Create button Form_plot_linear_Button108 child of Form28_MainPanel
   Form_plot_linear_Button108 = gmCreatePushButton(Form28_MainPanel, 190, 370, 75, 25, 'Both planes', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button108, (Form_plot_linear_Button108))
  
! Create value entry Form_plot_linear_Value98 child of Form28_MainPanel
   Form_plot_linear_Value98 = gmCreateValueEntry(Form28_MainPanel, 370, 370, 30, 25, 1.00000, 2, 0, GEDIT, gmVmin=-3.00000, &
      gmVmax=3.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form_plot_linear_Button109 child of Form28_MainPanel
   Form_plot_linear_Button109 = gmCreatePushButton(Form28_MainPanel, 440, 370, 75, 25, 'Reset', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button109, (Form_plot_linear_Button109))
  
! Create button Form_plot_linear_Button110 child of Form28_MainPanel
   Form_plot_linear_Button110 = gmCreatePushButton(Form28_MainPanel, 640, 370, 150, 25, 'Compute Phase Plot', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button110, (Form_plot_linear_Button110))
  
! Create button Form_plot_linear_Button111 child of Form28_MainPanel
   Form_plot_linear_Button111 = gmCreatePushButton(Form28_MainPanel, 990, 370, 75, 25, 'Plot Q-P', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button111, (Form_plot_linear_Button111))
  
! Create button Form_plot_linear_Button112 child of Form28_MainPanel
   Form_plot_linear_Button112 = gmCreatePushButton(Form28_MainPanel, 930, 170, 75, 25, 'Scale BPM', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button112, (Form_plot_linear_Button112))
  
! Create button Form_plot_linear_Button113 child of Form28_MainPanel
   Form_plot_linear_Button113 = gmCreatePushButton(Form28_MainPanel, 720, 340, 125, 25, 'Put Start at BPM', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button113, (Form_plot_linear_Button113))
  
! Create button Form_plot_linear_Button115 child of Form28_MainPanel
   Form_plot_linear_Button115 = gmCreatePushButton(Form28_MainPanel, 800, 370, 100, 25, 'Lattice functions', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button115, (Form_plot_linear_Button115))
  
! Create button Form_plot_linear_Button116 child of Form28_MainPanel
   Form_plot_linear_Button116 = gmCreatePushButton(Form28_MainPanel, 910, 370, 75, 25, 'Apply A', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button116, (Form_plot_linear_Button116))
  
! Create button Form_plot_linear_with_c child of Form28_MainPanel
   Form_plot_linear_with_c = gmCreatePushButton(Form28_MainPanel, 540, 370, 95, 25, 'Ignore coupling', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_with_c, (Form_plot_linear_with_c))
  
! Create button Form_plot_linear_all child of Form28_MainPanel
   Form_plot_linear_all = gmCreatePushButton(Form28_MainPanel, 940, 230, 75, 25, 'All planes', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_all, (Form_plot_linear_all))
  
! Create button Form_plot_linear_compute_all child of Form28_MainPanel
   Form_plot_linear_compute_all = gmCreatePushButton(Form28_MainPanel, 930, 270, 150, 25, 'Compute All', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_compute_all, (Form_plot_linear_compute_all))
  
! Create button Form_plot_linear_comp_lat child of Form28_MainPanel
   Form_plot_linear_comp_lat = gmCreatePushButton(Form28_MainPanel, 930, 310, 130, 25, 'All Lattices Functions', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_comp_lat, (Form_plot_linear_comp_lat))
  
! Create button Form_plot_linear_Button124 child of Form28_MainPanel
   Form_plot_linear_Button124 = gmCreatePushButton(Form28_MainPanel, 1020, 60, 75, 25, 'Plot Data All', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button124, (Form_plot_linear_Button124))
   call gmDefineSelectCallback(Form_plot_linear_Button124,(Form_plot_linear_Button124+GIDENT))
  
! Create button Form_plot_linear_ignore child of Form28_MainPanel
   Form_plot_linear_ignore = gmCreatePushButton(Form28_MainPanel, 1020, 100, 80, 25, 'Ignore bpm', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_ignore, (Form_plot_linear_ignore))
  
! Create button Form_plot_linear_list child of Form28_MainPanel
   Form_plot_linear_list = gmCreatePushButton(Form28_MainPanel, 1020, 130, 80, 40, 'List excluded bpm', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_list, (Form_plot_linear_list))
  
! Create button Form_plot_linear_Button127 child of Form28_MainPanel
   Form_plot_linear_Button127 = gmCreatePushButton(Form28_MainPanel, 1010, 180, 80, 40, 'Read list excluded bpm', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_plot_linear_Button127, (Form_plot_linear_Button127))
return
end subroutine
subroutine GenerateForm30_twiss
  
! Set up complex dialogue box Form30_twiss child of Form1
   Form30_twiss = gmCreateComplexDialogueBox(Form1, 240, 249, 582, 242, GALL, 'Initial Twiss Values', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='Gee')
  
! Create main panel for form
   Form30_MainPanel=gmCreatePanel(Form30_twiss, 0, 0, 582, 242, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create value entry Form30_twiss_alx child of Form30_MainPanel
   Form30_twiss_alx = gmCreateValueEntry(Form30_MainPanel, 70, 60, 100, 25, 0.000000, 14, 8, GEDIT, gmVmin=0.000000, &
      gmVmax=0.000000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_alx, (Form30_twiss_alx))
  
! Create text widget Form30_twiss_Text75 child of Form30_MainPanel
   Form30_twiss_Text75 = gmCreateTextEntry(Form30_MainPanel, 20, 60, 50, 25,'alpha x ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form30_twiss_betax child of Form30_MainPanel
   Form30_twiss_betax = gmCreateValueEntry(Form30_MainPanel, 70, 20, 100, 25, 0.000000, 14, 8, GEDIT, gmVmin=0.000000, &
      gmVmax=0.000000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_betax, (Form30_twiss_betax))
  
! Create text widget Form30_twiss_Text76 child of Form30_MainPanel
   Form30_twiss_Text76 = gmCreateTextEntry(Form30_MainPanel, 20, 20, 50, 25,'beta x ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form30_twiss_aly child of Form30_MainPanel
   Form30_twiss_aly = gmCreateValueEntry(Form30_MainPanel, 70, 150, 100, 25, 0.000000, 14, 8, GEDIT, gmVmin=0.000000, &
      gmVmax=0.000000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_aly, (Form30_twiss_aly))
  
! Create text widget Form30_twiss_Text77 child of Form30_MainPanel
   Form30_twiss_Text77 = gmCreateTextEntry(Form30_MainPanel, 20, 150, 50, 25,'alpha y ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form30_twiss_betay child of Form30_MainPanel
   Form30_twiss_betay = gmCreateValueEntry(Form30_MainPanel, 70, 110, 100, 25, 0.000000, 14, 8, GEDIT, gmVmin=0.000000, &
      gmVmax=0.000000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_betay, (Form30_twiss_betay))
  
! Create text widget Form30_twiss_Text78 child of Form30_MainPanel
   Form30_twiss_Text78 = gmCreateTextEntry(Form30_MainPanel, 20, 110, 50, 25,'beta y ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form30_twiss_Button89 child of Form30_MainPanel
   Form30_twiss_Button89 = gmCreatePushButton(Form30_MainPanel, 230, 180, 125, 25, 'Fill in from computed A', gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form30_twiss_Button89, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form30_twiss_Button89, (Form30_twiss_Button89))
  
! Create button Form30_twiss_make_ait child of Form30_MainPanel
   Form30_twiss_make_ait = gmCreatePushButton(Form30_MainPanel, 40, 180, 160, 25, 'Make A using above data', gmVpos=GTOP, &
      gmTabList=GON)
   call gmSetFontAttribs(Form30_twiss_make_ait, gmFontFace='MS Sans Serif', gmPointSize=8)
   call gmDefineCallback(Form30_twiss_make_ait, (Form30_twiss_make_ait))
  
! Create text widget Form30_twiss_qf child of Form30_MainPanel
   Form30_twiss_qf = gmCreateTextEntry(Form30_MainPanel, 310, 30, 100, 25,'QF ', 30000, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_qf, (Form30_twiss_qf))
  
! Create text widget Form30_twiss_qd child of Form30_MainPanel
   Form30_twiss_qd = gmCreateTextEntry(Form30_MainPanel, 310, 70, 100, 25,'QD ', 30000, GEDIT, gmBack1Col=0, gmBack2Col=0, &
      gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_qd, (Form30_twiss_qd))
  
! Create value entry Form30_twiss_qx child of Form30_MainPanel
   Form30_twiss_qx = gmCreateValueEntry(Form30_MainPanel, 310, 110, 100, 25, 0.000000, 14, 8, GEDIT, gmVmin=-1.00000, &
      gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_qx, (Form30_twiss_qx))
  
! Create value entry Form30_twiss_qy child of Form30_MainPanel
   Form30_twiss_qy = gmCreateValueEntry(Form30_MainPanel, 310, 140, 100, 25, 0.000000, 14, 8, GEDIT, gmVmin=-1.00000, &
      gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_qy, (Form30_twiss_qy))
  
! Create text widget Form30_twiss_Text88 child of Form30_MainPanel
   Form30_twiss_Text88 = gmCreateTextEntry(Form30_MainPanel, 240, 30, 50, 25,'QF ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form30_twiss_Text89 child of Form30_MainPanel
   Form30_twiss_Text89 = gmCreateTextEntry(Form30_MainPanel, 240, 70, 50, 25,'QD ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form30_twiss_Text90 child of Form30_MainPanel
   Form30_twiss_Text90 = gmCreateTextEntry(Form30_MainPanel, 240, 110, 50, 25,'Tune X ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form30_twiss_Text91 child of Form30_MainPanel
   Form30_twiss_Text91 = gmCreateTextEntry(Form30_MainPanel, 240, 140, 50, 25,'Tune Y ', 255, GDISPLAY, gmBack1Col=0, &
      gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create button Form30_twiss_fit_tunes child of Form30_MainPanel
   Form30_twiss_fit_tunes = gmCreatePushButton(Form30_MainPanel, 420, 90, 75, 25, 'Fit Tunes', gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_fit_tunes, (Form30_twiss_fit_tunes))
  
! Create value entry Form30_twiss_epsfr child of Form30_MainPanel
   Form30_twiss_epsfr = gmCreateValueEntry(Form30_MainPanel, 430, 50, 68, 25, 1.00000e-005, 14, 8, GEDIT, gmVmin=0.000000, &
      gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form30_twiss_epsfr, (Form30_twiss_epsfr))
return
end subroutine
subroutine GenerateForm_resonance
  
! Set up complex dialogue box Form_resonance child of Form1
   Form_resonance = gmCreateComplexDialogueBox(Form1, 298, 110, 1179, 676, GALL, 'Resonance 4d', gmVpos=GTOP, &
      gmQuitMode=GHIDE, gmInitState=GNORMAL, gmIconFormat=GDLLICON, gmIconFile='Gee')
   call gmDefineCallback(Form_resonance, (Form_resonance))
   call gmDefineSelectCallback(Form_resonance,(Form_resonance+GIDENT))
  
! Create main panel for form
   Form_resonance_MainPanel=gmCreatePanel(Form_resonance, 0, 0, 1179, 676, gmHpos=GCENTRE, gmVpos=GMIDDLE, gmExpand=GON, &
      gmType=GNOBOUNDARY, gmLineBorder=GOUTEREDGE, gmFillBorder=GOUTEREDGE)
  
! Create graphics frame Form_resonance_plot_phi_i child of Form_resonance_MainPanel
   Form_resonance_plot_phi_i = gmCreateGraphicsFrame(Form_resonance_MainPanel, 60, 30, 670, 570, &
         GLIMIT(0.000000, 6.290000, 0.000000, 100.000000), gmFrameType=GGDI, gmVpos=GTOP)
   call gmDefineCallback(Form_resonance_plot_phi_i, (Form_resonance_plot_phi_i))
   call gmDefineSelectCallback(Form_resonance_plot_phi_i,(Form_resonance_plot_phi_i+GIDENT))
  
! Create value entry Form_resonance_mr1 child of Form_resonance_MainPanel
   Form_resonance_mr1 = gmCreateValueEntry(Form_resonance_MainPanel, 780, 80, 110, 25, 0.000000, 16, 8, GEDIT, gmVmin=7.00000, &
      gmVmax=7.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_mr1, (Form_resonance_mr1))
  
! Create value entry Form_resonance_mr2 child of Form_resonance_MainPanel
   Form_resonance_mr2 = gmCreateValueEntry(Form_resonance_MainPanel, 930, 80, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-7.00000, gmVmax=7.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_mr2, (Form_resonance_mr2))
  
! Create value entry Form_resonance_dmr1 child of Form_resonance_MainPanel
   Form_resonance_dmr1 = gmCreateValueEntry(Form_resonance_MainPanel, 760, 140, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-700000., gmVmax=700000., gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_dmr1, (Form_resonance_dmr1))
  
! Create value entry Form_resonance_dmr2 child of Form_resonance_MainPanel
   Form_resonance_dmr2 = gmCreateValueEntry(Form_resonance_MainPanel, 880, 140, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-700000., gmVmax=700000., gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_dmr2, (Form_resonance_dmr2))
  
! Create value entry Form_resonance_dmr3 child of Form_resonance_MainPanel
   Form_resonance_dmr3 = gmCreateValueEntry(Form_resonance_MainPanel, 1000, 140, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-700000., gmVmax=700000., gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_dmr3, (Form_resonance_dmr3))
  
! Create value entry Form_resonance_a child of Form_resonance_MainPanel
   Form_resonance_a = gmCreateValueEntry(Form_resonance_MainPanel, 780, 210, 100, 25, 0.000000, 16, 8, GEDIT, gmVmin=-700000., &
      gmVmax=700000., gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_a, (Form_resonance_a))
  
! Create value entry Form_resonance_phi child of Form_resonance_MainPanel
   Form_resonance_phi = gmCreateValueEntry(Form_resonance_MainPanel, 930, 210, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=0.000000, gmVmax=6.29000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_phi, (Form_resonance_phi))
  
! Create button Form_resonance_compute child of Form_resonance_MainPanel
   Form_resonance_compute = gmCreatePushButton(Form_resonance_MainPanel, 950, 290, 115, 35, 'compute resonance', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_compute, (Form_resonance_compute))
   call gmDefineSelectCallback(Form_resonance_compute,(Form_resonance_compute+GIDENT))
  
! Create value entry Form_resonance_sum child of Form_resonance_MainPanel
   Form_resonance_sum = gmCreateValueEntry(Form_resonance_MainPanel, 780, 300, 100, 25, 0.000000, 8, 0, GEDIT, &
      gmVmin=-1.00000, gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_sum, (Form_resonance_sum))
  
! Create text widget Form_resonance_Text92 child of Form_resonance_MainPanel
   Form_resonance_Text92 = gmCreateTextEntry(Form_resonance_MainPanel, 780, 50, 250, 25,'Double Sum and Difference of Tunes ', &
      255, GDISPLAY, gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create text widget Form_resonance_Text93 child of Form_resonance_MainPanel
   Form_resonance_Text93 = gmCreateTextEntry(Form_resonance_MainPanel, 800, 110, 250, 25,'Tune shifts with amplitudes ', 255, &
      GDISPLAY, gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, &
      gmTabList=GON)
  
! Create text widget Form_resonance_Text94 child of Form_resonance_MainPanel
   Form_resonance_Text94 = gmCreateTextEntry(Form_resonance_MainPanel, 780, 180, 110, 25,'Resonance Strength ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_resonance_Text95 child of Form_resonance_MainPanel
   Form_resonance_Text95 = gmCreateTextEntry(Form_resonance_MainPanel, 930, 180, 110, 25,'Phase ', 255, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_resonance_Text97 child of Form_resonance_MainPanel
   Form_resonance_Text97 = gmCreateTextEntry(Form_resonance_MainPanel, 780, 270, 120, 25,'Sum=1 difference = -1 ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_resonance_J child of Form_resonance_MainPanel
   Form_resonance_J = gmCreateValueEntry(Form_resonance_MainPanel, 780, 360, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GSCIENTIFIC, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_J, (Form_resonance_J))
  
! Create text widget Form_resonance_Text98 child of Form_resonance_MainPanel
   Form_resonance_Text98 = gmCreateTextEntry(Form_resonance_MainPanel, 770, 330, 140, 25,'J_perp = a.J where a.m=0 ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_resonance_jsol1 child of Form_resonance_MainPanel
   Form_resonance_jsol1 = gmCreateValueEntry(Form_resonance_MainPanel, 780, 420, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GSCIENTIFIC, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_jsol1, (Form_resonance_jsol1))
  
! Create value entry Form_resonance_jsol2 child of Form_resonance_MainPanel
   Form_resonance_jsol2 = gmCreateValueEntry(Form_resonance_MainPanel, 930, 420, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GSCIENTIFIC, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_jsol2, (Form_resonance_jsol2))
  
! Create button Form_resonance_Button129 child of Form_resonance_MainPanel
   Form_resonance_Button129 = gmCreatePushButton(Form_resonance_MainPanel, 960, 340, 90, 25, 'Compute J_Res', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_Button129, (Form_resonance_Button129))
  
! Create text widget Form_resonance_Text99 child of Form_resonance_MainPanel
   Form_resonance_Text99 = gmCreateTextEntry(Form_resonance_MainPanel, 840, 390, 130, 25,'Fixed points for J_perp=0 ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_resonance_dt child of Form_resonance_MainPanel
   Form_resonance_dt = gmCreateValueEntry(Form_resonance_MainPanel, 780, 620, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000, gmVmax=1.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_dt, (Form_resonance_dt))
  
! Create text widget Form_resonance_Text100 child of Form_resonance_MainPanel
   Form_resonance_Text100 = gmCreateTextEntry(Form_resonance_MainPanel, 780, 590, 100, 25,'dt for Integration ', 255, &
      GDISPLAY, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create value entry Form_resonance_psi0 child of Form_resonance_MainPanel
   Form_resonance_psi0 = gmCreateValueEntry(Form_resonance_MainPanel, 780, 490, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-7.00000, gmVmax=7.00000, gmType=GDECIMAL, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_psi0, (Form_resonance_psi0))
  
! Create value entry Form_resonance_i0 child of Form_resonance_MainPanel
   Form_resonance_i0 = gmCreateValueEntry(Form_resonance_MainPanel, 940, 490, 100, 25, 0.000000, 16, 8, GEDIT, &
      gmVmin=-1.00000e+006, gmVmax=1.00000e+006, gmType=GSCIENTIFIC, gmBack1Col=0, gmBack2Col=0, gmTextCol=0, gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_i0, (Form_resonance_i0))
  
! Create button Form_resonance_Button130 child of Form_resonance_MainPanel
   Form_resonance_Button130 = gmCreatePushButton(Form_resonance_MainPanel, 780, 540, 160, 25, 'Use Ray from nd plots', &
      gmVpos=GTOP, gmTabList=GON)
   call gmDefineCallback(Form_resonance_Button130, (Form_resonance_Button130))
  
! Create button Form_resonance_Button131 child of Form_resonance_MainPanel
   Form_resonance_Button131 = gmCreatePushButton(Form_resonance_MainPanel, 990, 540, 75, 25, 'Send to ray', gmVpos=GTOP, &
      gmTabList=GON)
   call gmDefineCallback(Form_resonance_Button131, (Form_resonance_Button131))
  
! Create text widget Form_resonance_Text101 child of Form_resonance_MainPanel
   Form_resonance_Text101 = gmCreateTextEntry(Form_resonance_MainPanel, 780, 460, 100, 25,'Psi ', 30000, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_resonance_Text102 child of Form_resonance_MainPanel
   Form_resonance_Text102 = gmCreateTextEntry(Form_resonance_MainPanel, 940, 460, 100, 25,'I ', 30000, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_resonance_Text103 child of Form_resonance_MainPanel
   Form_resonance_Text103 = gmCreateTextEntry(Form_resonance_MainPanel, 1040, 490, 60, 25,'mm*mrad ', 30000, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_resonance_Text104 child of Form_resonance_MainPanel
   Form_resonance_Text104 = gmCreateTextEntry(Form_resonance_MainPanel, 1040, 420, 60, 25,'mm*mrad ', 30000, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
  
! Create text widget Form_resonance_Text105 child of Form_resonance_MainPanel
   Form_resonance_Text105 = gmCreateTextEntry(Form_resonance_MainPanel, 890, 360, 60, 25,'mm*mrad ', 30000, GDISPLAY, &
      gmJustify=GCENTRE, gmBack1Col=0, gmBack2Col=12, gmTextCol=1 ,gmScrollMode=GVERTICAL, gmVpos=GTOP, gmTabList=GON)
return
end subroutine
! GetMasterFormID Function
   Integer Function GetMasterFormID()
   GetMasterFormID = Form1
   end function
End Module
 subroutine Call_gino(gino_command) 
  
use mstudio
use menu_f90
  
! User defined variables
implicit none
  type (GACTION)  :: actlist
  type (graph_data)  :: ray
  integer i,I2
logical done
integer flag_layout
integer flag_layout_default
real(sp) near(2,3),far(2,3),kob(2,3),kob0(2,3)
real(sp) val
real(dp) xbn
integer npole
integer i_lat,j_lat,k_lat,dis_pos
INTEGER :: n_lat=4
character(nlp) dis_name,name
type(fibre),pointer:: fib
real(dp) q(3,0:6),dtune(3),fix3(6)
real x,y,dh,dv,x2,CG(2)
integer :: turns=1
type(pol_block), target:: pol(20)
integer, target :: n_pol
type(GWIDGET) GWID
  
real(dp) :: targ_mf(2)=(/0.68_dp,0.27_dp/), incre(2)
character(256) gm_label
type(glimit) limit_temp
character*120 file_res
  
!integer, parameter :: nbmp=100
!character*120 bpmfile(nbmp)
!character*120 bpmfile_init(nbmp)
!integer :: ibpm
integer :: mstable=0
integer :: unmstable=0
character*255 filestableb
character*255 filestable
character*255 unfilestableb
character*255 unfilestable
integer mwarn
 character(*) gino_command 
character*120 videoname
integer videodevice,istatus
!logical :: opened, exists
integer :: first_gino=1
save first_gino
integer th(7),mti,nraysfile
real(dp), allocatable :: track_data_f(:,:)
integer icode,code,dcode
real(dp) xsync(6),d(3),ang(3),al_(2),bet_(2),ai0(6,6)
integer :: track_accel=100001,time_accel=200,ncode=100
integer :: track_plot=100002,time_plot=1,iplot=0
integer :: girder_flash=100003,time_girder=100,ngirder=0
integer :: siamese_flash=100004,time_siamese=100,nsiamese=0
integer :: track_lin=100005,time_lin=100,ilin=0
integer :: girder_colour(2)
integer :: siamese_colour(2)
character*255 :: plot1 = 'x\plot_x_px_'
character*255 :: plot2 = 'y\plot_y_py_'
character*255 :: plot3 = 't\plot_t_pt_'
character*255 :: plotmovie(3)
character*255:: combo_entry
integer pixbuf(48000),xgrid,ygrid
logical exists,adderror
integer kplot,accel_method
integer i1,k_cav_plot,jn(4)
type(internal_state) TEMP_STATE
integer  :: angx = 0,angy = 0,angz = 0
type(gpixframe) gpix
type(glimit) glim
real(dp) targt(2),epsfr
character(nlp) nametg(2)
TYPE(LAYOUT),POINTER :: L,l_dummy
integer nperring,ndelta,ifile,nturn_dejan,kplotl,kplots,kplote
logical :: didlat=.false.
type(GWIDGET) prop_widget
integer :: col_off=2
integer :: col_on=5
real(dp) glob(3,3)
real dx(2),dy(2),dl,dl0
  
  
! Initialise Gino, Device & Menu
 if(first_gino==1) then 
   call gOpenGino
   call gOglwin
   call gmInitializeMenu
  
! Define required grid
   call gmDefineGuiGrid(36,27)
   call gmSetGuiGridMode(0)
  
! Define user defined colour indices used
   call gDefineRGB(20, 0.941176, 0.972549, 1.000000)
   call gDefineRGB(21, 0.980392, 0.921569, 0.843137)
   call gDefineRGB(22, 0.000000, 1.000000, 1.000000)
   call gDefineRGB(23, 0.498039, 1.000000, 0.831373)
   call gDefineRGB(24, 0.941176, 1.000000, 1.000000)
   call gDefineRGB(25, 0.960784, 0.960784, 0.862745)
   call gDefineRGB(26, 1.000000, 0.894118, 0.768627)
   call gDefineRGB(27, 1.000000, 0.921569, 0.803922)
   call gDefineRGB(28, 0.541176, 0.168627, 0.886275)
   call gDefineRGB(29, 0.870588, 0.721569, 0.529412)
   call gDefineRGB(30, 0.372549, 0.619608, 0.627451)
   call gDefineRGB(31, 0.498039, 1.000000, 0.000000)
   call gDefineRGB(32, 0.823529, 0.411765, 0.117647)
   call gDefineRGB(33, 1.000000, 0.498039, 0.313726)
   call gDefineRGB(34, 0.392157, 0.584314, 0.929412)
   call gDefineRGB(35, 1.000000, 0.972549, 0.862745)
   call gDefineRGB(36, 0.929412, 0.643137, 0.239216)
   call gDefineRGB(37, 0.000000, 0.000000, 0.545098)
   call gDefineRGB(38, 0.000000, 0.545098, 0.545098)
   call gDefineRGB(39, 0.721569, 0.525490, 0.043137)
   call gDefineRGB(40, 0.662745, 0.662745, 0.662745)
   call gDefineRGB(41, 0.000000, 0.392157, 0.000000)
   call gDefineRGB(42, 0.741176, 0.717647, 0.419608)
   call gDefineRGB(43, 0.545098, 0.000000, 0.545098)
   call gDefineRGB(44, 0.333333, 0.419608, 0.184314)
   call gDefineRGB(45, 1.000000, 0.549020, 0.000000)
   call gDefineRGB(46, 0.600000, 0.196078, 0.800000)
   call gDefineRGB(47, 0.545098, 0.000000, 0.000000)
   call gDefineRGB(48, 0.913725, 0.588235, 0.478431)
   call gDefineRGB(49, 0.560784, 0.737255, 0.560784)
   call gDefineRGB(50, 0.282353, 0.239216, 0.545098)
   call gDefineRGB(51, 0.184314, 0.309804, 0.309804)
   call gDefineRGB(52, 0.000000, 0.807843, 0.819608)
   call gDefineRGB(53, 0.580392, 0.000000, 0.827451)
   call gDefineRGB(54, 1.000000, 0.078431, 0.576471)
   call gDefineRGB(55, 0.000000, 0.749020, 1.000000)
   call gDefineRGB(56, 0.411765, 0.411765, 0.411765)
   call gDefineRGB(57, 0.117647, 0.564706, 1.000000)
   call gDefineRGB(58, 0.698039, 0.133333, 0.133333)
   call gDefineRGB(59, 1.000000, 0.980392, 0.941176)
   call gDefineRGB(60, 0.133333, 0.545098, 0.133333)
   call gDefineRGB(61, 1.000000, 0.000000, 1.000000)
   call gDefineRGB(62, 0.862745, 0.862745, 0.862745)
   call gDefineRGB(63, 0.972549, 0.972549, 1.000000)
   call gDefineRGB(64, 1.000000, 0.843137, 0.000000)
   call gDefineRGB(65, 0.501961, 0.501961, 0.501961)
   call gDefineRGB(66, 0.678431, 1.000000, 0.184314)
   call gDefineRGB(67, 0.941176, 1.000000, 0.941176)
   call gDefineRGB(68, 1.000000, 0.411765, 0.705882)
   call gDefineRGB(69, 0.803922, 0.360784, 0.360784)
   call gDefineRGB(70, 0.294118, 0.000000, 0.509804)
   call gDefineRGB(71, 1.000000, 1.000000, 0.941176)
   call gDefineRGB(72, 0.941176, 0.901961, 0.549020)
   call gDefineRGB(73, 0.901961, 0.901961, 0.980392)
   call gDefineRGB(74, 0.486275, 0.988235, 0.000000)
   call gDefineRGB(75, 1.000000, 0.980392, 0.803922)
   call gDefineRGB(76, 0.678431, 0.847059, 0.901961)
   call gDefineRGB(77, 0.941176, 0.501961, 0.501961)
   call gDefineRGB(78, 0.878431, 1.000000, 1.000000)
   call gDefineRGB(79, 0.980392, 0.980392, 0.823529)
   call gDefineRGB(80, 0.827451, 0.827451, 0.827451)
   call gDefineRGB(81, 0.564706, 0.933333, 0.564706)
   call gDefineRGB(82, 1.000000, 0.713726, 0.756863)
   call gDefineRGB(83, 0.125490, 0.698039, 0.666667)
   call gDefineRGB(84, 0.529412, 0.807843, 0.980392)
   call gDefineRGB(85, 0.466667, 0.533333, 0.600000)
   call gDefineRGB(86, 0.690196, 0.768627, 0.870588)
   call gDefineRGB(87, 1.000000, 1.000000, 0.878431)
   call gDefineRGB(88, 0.000000, 1.000000, 0.000000)
   call gDefineRGB(89, 0.196078, 0.803922, 0.196078)
   call gDefineRGB(90, 0.980392, 0.941176, 0.901961)
   call gDefineRGB(91, 0.501961, 0.000000, 0.000000)
   call gDefineRGB(92, 0.400000, 0.803922, 0.666667)
   call gDefineRGB(93, 0.000000, 0.000000, 0.803922)
   call gDefineRGB(94, 0.729412, 0.333333, 0.827451)
   call gDefineRGB(95, 0.576471, 0.439216, 0.858824)
   call gDefineRGB(96, 0.235294, 0.701961, 0.443137)
   call gDefineRGB(97, 0.482353, 0.407843, 0.933333)
   call gDefineRGB(98, 0.000000, 0.980392, 0.603922)
   call gDefineRGB(99, 0.282353, 0.819608, 0.800000)
   call gDefineRGB(100, 0.780392, 0.082353, 0.521569)
   call gDefineRGB(101, 0.098039, 0.098039, 0.439216)
   call gDefineRGB(102, 0.960784, 1.000000, 0.980392)
   call gDefineRGB(103, 1.000000, 0.894118, 0.882353)
   call gDefineRGB(104, 1.000000, 0.894118, 0.709804)
   call gDefineRGB(105, 1.000000, 0.870588, 0.678431)
   call gDefineRGB(106, 0.000000, 0.000000, 0.501961)
   call gDefineRGB(107, 0.992157, 0.960784, 0.901961)
   call gDefineRGB(108, 0.501961, 0.501961, 0.000000)
   call gDefineRGB(109, 0.419608, 0.556863, 0.137255)
   call gDefineRGB(110, 1.000000, 0.270588, 0.000000)
   call gDefineRGB(111, 0.854902, 0.439216, 0.839216)
   call gDefineRGB(112, 0.933333, 0.909804, 0.666667)
   call gDefineRGB(113, 0.596078, 0.984314, 0.596078)
   call gDefineRGB(114, 0.686275, 0.933333, 0.933333)
   call gDefineRGB(115, 0.858824, 0.439216, 0.576471)
   call gDefineRGB(116, 1.000000, 0.937255, 0.835294)
   call gDefineRGB(117, 1.000000, 0.854902, 0.725490)
   call gDefineRGB(118, 0.803922, 0.521569, 0.247059)
   call gDefineRGB(119, 1.000000, 0.752941, 0.796078)
   call gDefineRGB(120, 0.866667, 0.627451, 0.866667)
   call gDefineRGB(121, 0.690196, 0.878431, 0.901961)
   call gDefineRGB(122, 0.501961, 0.000000, 0.501961)
   call gDefineRGB(123, 0.737255, 0.560784, 0.560784)
   call gDefineRGB(124, 0.254902, 0.411765, 0.882353)
   call gDefineRGB(125, 0.545098, 0.270588, 0.074510)
   call gDefineRGB(126, 0.980392, 0.501961, 0.447059)
   call gDefineRGB(127, 0.180392, 0.545098, 0.341176)
   call gDefineRGB(128, 1.000000, 0.960784, 0.933333)
   call gDefineRGB(129, 0.627451, 0.321569, 0.176471)
   call gDefineRGB(130, 0.752941, 0.752941, 0.752941)
   call gDefineRGB(131, 0.529412, 0.807843, 0.921569)
   call gDefineRGB(132, 0.415686, 0.352941, 0.803922)
   call gDefineRGB(133, 0.439216, 0.501961, 0.564706)
   call gDefineRGB(134, 1.000000, 0.980392, 0.980392)
   call gDefineRGB(135, 0.000000, 1.000000, 0.498039)
   call gDefineRGB(136, 0.274510, 0.509804, 0.705882)
   call gDefineRGB(137, 0.823529, 0.705882, 0.549020)
   call gDefineRGB(138, 0.000000, 0.501961, 0.501961)
   call gDefineRGB(139, 0.847059, 0.749020, 0.847059)
   call gDefineRGB(140, 1.000000, 0.388235, 0.278431)
   call gDefineRGB(141, 0.250980, 0.878431, 0.815686)
   call gDefineRGB(142, 0.933333, 0.509804, 0.933333)
   call gDefineRGB(143, 0.960784, 0.870588, 0.701961)
   call gDefineRGB(144, 0.960784, 0.960784, 0.960784)
   call gDefineRGB(145, 0.603922, 0.803922, 0.196078)
  
! Generate Forms
   call GenerateForm1
   call GenerateForm_frame_size
   call GenerateControls
   call Generatex1_x2
   call Generatex3_x4
   call Generatex5_x6
   call GeneratePLOT_ring
   call GenerateForm_States
   call GenerateForm_orbit
   call GenerateForm_scale
   call GenerateForm_mis
   call GenerateForm_lat
   call GenerateForm_dyn
   call GenerateForm_xpx_n
   call GenerateForm_ypy_n
   call GenerateForm_explain
   call GenerateForm_warn
   call GenerateForm_explain_layout
   call GenerateForm_help_dyn
   call GenerateForm_warn_gauge
   call GenerateForm_magnet_scale
   call GeneratePlot_3D
   call GenerateForm_plot_linear
   call GenerateForm30_twiss
   call GenerateForm_resonance
  
  write(6,*) " Initializing Gino internal engine "
 USE_INFO=.TRUE. 
 FIRST_GINO=2 
 RETURN 
 endif 
! Initialization code
if(associated(my_e_beam)) then
 call gmSetWidgetStatus(Controls_plot_e_beam, GSELECTABLE)
 call gmSetWidgetStatus(Controls_size_e_beam, GSELECTABLE)
 call gmSetWidgetStatus(Controls_track_e, GSELECTABLE)
 call gmSetWidgetStatus(Controls_start_lin, GSELECTABLE)
 call gmSetWidgetStatus(Controls_stop_lin, GSELECTABLE)
  
else
 call gmSetWidgetStatus(Controls_plot_e_beam, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_size_e_beam, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_track_e, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_start_lin, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_stop_lin, GunSELECTABLE)
  
endif
 call gmSetWidgetStatus(Controls_Button134, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_Button135, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_Button136, GunSELECTABLE)
 call gmSetWidgetStatus(Controls_Button137, GunSELECTABLE)
  
dl=0.0;dl0=0.0;
 but1=Form_lat_Button99
 but2=Form_lat_Button100
         call gmSetWidgetStatus(Form_lat_Button99, GUNSELECTABLE)
         call gmSetWidgetStatus(Form_lat_Button100, GUNSELECTABLE)
if(.not.didlat) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
IF(gino_command(1:9)=='CLOSEGINO') THEN
 FIRST_GINO=100
 WRITE(6,*) " Closing Gino's internal engines "
 GOTO 9999
ENDIF
nullify(p_gino_1)
nullify(p_gino_2)
  
cg=0.0
use_info=.TRUE.
 call set_lattice_pointers 
  
WRITE(6,*) "use_info ",use_info
write(6,*) " Number of Layout in M_u ", m_u%n
write(6,*) " Number of Layout in M_t ", m_t%n
  
  
 use_info=.true.
call gmSetWidgetStatus(Form_frame_size_scale, GUNSELECTABLE)
call gmSetWidgetStatus(m_vcontrol, GUNSELECTABLE)
call gmSetWidgetStatus(M_plotbetax, GUNSELECTABLE)
call gmSetWidgetStatus(m_scalebetax, GUNSELECTABLE)
call gmSetWidgetStatus(M_select, GUNSELECTABLE)
call gmSetWidgetStatus(M_auto_scale, GUNSELECTABLE)
call gmEnqWidgetInfo(M_plotbetax,gwid)
gwid%label="Plot : First Select Lattice Function"
call gmSetWidgetInfo(M_plotbetax,gwid)
  
!call gSetWorkspaceLimit(1,1000);
!call gDefinePolygonWorkspace(1000)
  
call set_gino_warnings
call read_pref
  
do i=1,10
 if(scriptfilenamelist(i)/=' ') then
  call gmSetListEntry(Controls_Combo_script, GADD, gmString=scriptfilenamelist(i))
 endif
enddo
  call gmSetTextSetting(Controls_scriptfilename , scriptfilenamelist(1))
  
call set_colours
plot(1)=x1_x2_Graphics1
form_plot(1)=x1_x2
plot(2)=x3_x4_Graphics2
form_plot(2)=x3_x4
plot(3)=x5_x6_Graphics3
form_plot(3)=x5_x6
plot(4)=PLOT_ring_Graphics4
form_plot(4)=PLOT_ring
plot(5)=Form_dyn_plot
form_plot(5)=Form_dyn
! normal
plot(6)=Form_xpx_n_g
form_plot(6)=Form_xpx_n
plot(7)=Form_ypy_n_g
form_plot(7)=Form_ypy_n
form_plot(8)=Plot_3d
plot(8)=Plot_3d_G
form_plot(9)=Form_plot_linear
plot(9)=Form_plot_linear_Graphics
form_plot(10)=Form_resonance
plot(10)=Form_resonance_plot_phi_i
wgbpm=Form_plot_linear_Graphics
wibpm=Form_plot_linear_ibpm
wnbpm=Form_plot_linear_nbpm
call gmSetTextSetting(Form_plot_linear_nbpm ,nbpm)
call gmSetValueSetting(Form_plot_linear_ibpm ,ibpm)
call gmSetValueSetting(Form_plot_linear_nturn ,m_turn )
call gmSetValueSetting(Form_plot_linear_Value98 ,-3 )
call gmSetValueSetting(Controls_n_mul ,1 )
if(with_c==0) then
call gmSetWidgetProp(Form_plot_linear_with_c,gmLabel="Use Coupling")
else
call gmSetWidgetProp(Form_plot_linear_with_c,gmLabel="Ignore Coupling")
endif
  
nametg(1)='QF'
nametg(2)='QD'
epsfr=1.d-5
targt(1)=0.33d0
targt(2)=0.75d0
call gmSetTextSetting(Form30_twiss_qf ,nametg(1) )
call gmSetTextSetting(Form30_twiss_qd ,nametg(2) )
call gmSetValueSetting(Form30_twiss_epsfr ,epsfr)
call gmSetValueSetting(Form30_twiss_epsfr ,epsfr)
call gmSetValueSetting(Form30_twiss_qx ,targt(1))
call gmSetValueSetting(Form30_twiss_qy ,targt(2))
  
  
plot_sizes_name(1)="X-PX Sizes"
plot_sizes_name(2)="Y-PY Sizes"
plot_sizes_name(3)="PT-T Sizes"
plot_sizes_name(4)="Layout of the System"
plot_sizes_name(5)="Select Initial Tracking Point"
plot_sizes_name(6)="Normalized X-PX Sizes"
plot_sizes_name(7)="Normalized Y-PY Sizes"
plot_sizes_name(8)="Plot 3D Sizes"
plot_sizes_name(9)="Plot Linear Graphs"
plot_sizes_name(10)="Plot Resonance I-PSI"
 flag_layout=1   ! plot trajectoty true makes it false!
flag_layout_default=1
  if(flag_layout/=0) then
   flag_layout=0
   call gmSetWidgetProp(m_plot_ray,gmLabel="Plot Ray")
  else
  flag_layout=flag_layout_default
  call gmSetWidgetProp(m_plot_ray,gmLabel="Do not Plot Ray")
  endif
coord(1)=Form_frame_size_xmin
coord(2)=Form_frame_size_xmax
coord(3)=Form_frame_size_ymin
coord(4)=Form_frame_size_ymax
this_plot=1
  
  
      call gmSetValueSetting(Controls_flag_layout ,flag_layout )
      call gmSetValueSetting(Controls_n_nd ,n_nd )
      call gmSetValueSetting(Controls_turns ,turns)
i_layout = m_u%n
       call gmSetValueSetting(Controls_layout_number,i_layout )
       call move_to_layout_i(m_u,my_ering,i_layout)
i_layout_t = m_t%n
       call gmSetValueSetting(Controls_m_t_number,i_layout_t )
if(m_t%n>0) then
	call move_to_layout_i(m_t,my_ering,i_layout_t)
      call gmEnqWidgetInfo(Controls_m_t_number, prop_widget)
       prop_widget%colour1=col_on
      call gmSetWidgetInfo(Controls_m_t_number, prop_widget)
      call gmEnqWidgetInfo(Controls_layout_number, prop_widget)
       prop_widget%colour1=col_off
      call gmSetWidgetInfo(Controls_layout_number, prop_widget)
else
      call gmEnqWidgetInfo(Controls_m_t_number, prop_widget)
       prop_widget%colour1=col_off
      call gmSetWidgetInfo(Controls_m_t_number, prop_widget)
      call gmEnqWidgetInfo(Controls_layout_number, prop_widget)
       prop_widget%colour1=col_on
      call gmSetWidgetInfo(Controls_layout_number, prop_widget)
endif
  
  
state(1)=Form_States_Totalpath
state(2)=Form_States_time
state(3)=Form_States_radiation
state(4)=Form_States_nocavity
state(5)=Form_States_fringe
state(6)=Form_States_exactmis
state(7)=Form_States_only4d
state(8)=Form_States_delta
state(9)=Form_States_fixpoint
state(10)=Form_States_fix0
state(11)=Form_States_ascript
state(12)=Form_States_aperture_check
state(13)=Form_States_track_map
state(14)=Form_States_stoch
state(15)=Controls_Toggle_random_colour
state(16)=Controls_orbit
state(17)=Controls_new_flat
state(18)=Form_States_spin
if(first_gino==2) then
 call clear_plots
 call set_plots_size
 first_gino=0
endif
! Misalignments
field_sig_mis(1,0)=Form_mis_10
field_sig_mis(2,0)=Form_mis_20
field_sig_mis(3,0)=Form_mis_30
field_sig_mis(1,1)=Form_mis_11
field_sig_mis(1,2)=Form_mis_12
field_sig_mis(1,3)=Form_mis_13
field_sig_mis(1,4)=Form_mis_14
field_sig_mis(1,5)=Form_mis_15
field_sig_mis(1,6)=Form_mis_16
field_sig_mis(2,1)=Form_mis_21
field_sig_mis(2,2)=Form_mis_22
field_sig_mis(2,3)=Form_mis_23
field_sig_mis(2,4)=Form_mis_24
field_sig_mis(2,5)=Form_mis_25
field_sig_mis(2,6)=Form_mis_26
field_sig_mis(3,1)=Form_mis_31
field_sig_mis(3,2)=Form_mis_32
field_sig_mis(3,3)=Form_mis_33
field_sig_mis(3,4)=Form_mis_34
field_sig_mis(3,5)=Form_mis_35
field_sig_mis(3,6)=Form_mis_36
! objectives 1 to 4 tunes and chroms
  
! Lattice functions
i_lat=1
j_lat=1
k_lat=1
n_lat=4
dis_pos=1
! local variables
ray=n_turns
  
call gmSetWidgetStatus(m_vcontrol, GSELECTABLE)	
  
file_res="output_resonance.txt"
mres(1)=0
mres(2)=0
mres(3)=0
mresp=0
call gmSetValueSetting(form_mres1 ,mres(1))
call gmSetValueSetting(form_mres2 ,mres(2))
call gmSetValueSetting(form_mres3 ,mres(3))
call gmSetValueSetting(Form_lat_resp ,mresp)
call gmSetValueSetting(Form_States_first_fib,first_fib )
call gmSetValueSetting(Form_States_second_fib,second_fib )
  
call gmSetTextSetting(Controls_scriptfilename,scriptfilename)
call gmSetTextSetting(Controls_flatin ,fileflatold)
call gmSetTextSetting(Controls_flatout ,fileflatnew)
call gmSetTextSetting(Controls_flatout ,fileflatnew)
call gmSetTextSetting( Form_lat_res_file, file_res)
call gmSetTextSetting( Form_lat_file, lattice_function_file)
  
!!!!!!!!!!!!!
      call gmSetValueSetting( Form_States_my_order, my_order)
      call gmSetValueSetting( Form_States_nturn, n_turns)
      call gmSetValueSetting( Form_States_my_delta, my_delta)
x_ap=-1000.0
y_ap=-1000.0
      call gmSetValueSetting( Form_States_apx, x_ap)
      call gmSetValueSetting( Form_States_apy, y_ap)
      call gmSetTextSetting(Form_States_name_ap ,name_ap )
      call gmSetTextSetting(Form_States_filename_ap ,filename_ap )
  
      call set_ptc_states(my_estate)
  
!!!!!!!!!!!
!       call kanalnummer(mstable)
       mstable=0
       filestable='stable_data.dat'
       call context(filestable)
!       open(unit=mstable,file=filestable)
                call gmSetTextSetting(Form_dyn_stable , filestable )
       unmstable=0
       unfilestable='unstable_data.dat'
       call context(unfilestable)
!       open(unit=unmstable,file=unfilestable)
                call gmSetTextSetting(Form_dyn_unstable , unfilestable )
  
call e_gm_draw_grid(gwhite,gblue,20,20,plot(5))
  
!WRITE(6,*) "THE GINO_COMMAND IS ",gino_command(1:3)
if(gino_command(1:3)=='MIN') THEN
      call gmDrawWindow(Controls )
      call gmPopWindow(Controls )
ELSEIF(gino_command(1:3)=='MAX') THEN
 do i=n_nd,1,-1
       call gmDrawWindow(form_plot(i) )
 enddo
      call gmDrawWindow(Controls )
      call gmDrawWindow(Form_States )
      call gmDrawWindow(form_dyn)
      call gmDrawWindow(PLOT_ring)
      call gmPopWindow(Controls )
ENDIF
  
call kanalnummer(mwarn)
open(unit=mwarn,file='warn.txt',status='OLD',err=2001)
 read(mwarn,*) authorwarn
close(mwarn)
2001 continue
if(authorwarn) then
 call gmDrawWindow(Form_warn)
endif
  
if(dirdotted) then
  
 call gmSetToggleSwitch(Controls_dotted ,gon )
else
 call gmSetToggleSwitch(Controls_dotted ,goff )
  
endif
  
if(select_gino_12==1) then
  
 call gmSetToggleSwitch(Form_States_fibre1 ,gon )
else
 call gmSetToggleSwitch(Form_States_fibre1 ,goff )
  
endif
if(lingyun_yang)then
 call gmSetToggleSwitch(Form_States_lingyun ,gon )
else
 call gmSetToggleSwitch(Form_States_lingyun ,goff )
endif
  
  
          ray%do_a_for_dyn=.false.
th(1)=Form_States_nst
th(2)=Form_States_t1
th(3)=Form_States_t2
th(4)=Form_States_start_t
th(5)=Form_States_Gauge_t
call fill_t(my_ering%start,th)
ray%tc=>my_start_t
 !th(6)=gmGetIconResource('MSML1061')
 !th(7)=gmGetIconResource('MSML1012')
 !th(8)=gmGetIconResource('MBIG1100')
ray%tc=0
  call gmSetTextSetting(Form_states_name ,my_ering%start%mag%name)
call warn_t(my_ering%start,Form_States_line)
         call gmSetValueSetting(Form_magnet_scale_scale ,dxmag )
         call gmSetValueSetting(Form_magnet_scale_Value117 ,dxframe )
nraysfile=0
         call gmSetTextSetting(Controls_track_file , trackfile )
icode=0
ncode=40
k_cav_plot =1
         call gmSetValueSetting(Controls_ncode , ncode)
kplot=3
         call gmSetValueSetting(Controls_kplot , kplot)
!               12345678901234567890123456789012345678901234567890
!plotmovie(1) = 'D:\document\etienne_programs\fake_mad\x\plot_x_px_'
!plotmovie(2) = 'D:\document\etienne_programs\fake_mad\y\plot_y_py_'
!plotmovie(3) = 'D:\document\etienne_programs\fake_mad\t\plot_t_pt_'
plotmovie(1) = 'x\plot_x_px_'
plotmovie(2) = 'y\plot_y_py_'
plotmovie(3) = 't\plot_t_pt_'
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  
girder_colour(1)=gred
girder_colour(2)=gblue
siamese_colour(1)=gwhite
siamese_colour(2)=gcyan
  
  
ait=0.d0
do i1=1,6
 ait(i1,i1)=1.d0
enddo
ai0=ait
bet_=2.5862d0
bet_(2)=2.5862d0
al_=-0.253157d0
      call gmSetValueSetting( Form30_twiss_betax, bet_(1))
      call gmSetValueSetting( Form30_twiss_betay, bet_(2))
      call gmSetValueSetting( Form30_twiss_alx, al_(1))
      call gmSetValueSetting( Form30_twiss_aly, al_(2))
mur=0.d0
dmur=0.d0
jsol=0.d0
A_str=0.d0
phi_str=0.d0
      call gmSetValueSetting( Form_resonance_mr1, mur(1))
      call gmSetValueSetting( Form_resonance_mr2, mur(2))
      call gmSetValueSetting( Form_resonance_dmr1, dmur(1))
      call gmSetValueSetting( Form_resonance_dmr2, dmur(2))
      call gmSetValueSetting( Form_resonance_dmr3, dmur(3))
      call gmSetValueSetting( Form_resonance_sum, sum_res_val)
     call gmSetValueSetting( Form_resonance_a, A_str)
     call gmSetValueSetting( Form_resonance_phi, phi_str)
     call gmSetValueSetting( Form_resonance_j, j_perp)
      call gmSetValueSetting( Form_resonance_jsol1, jsol(1))
      call gmSetValueSetting( Form_resonance_jsol2, jsol(2))
      call gmSetValueSetting( Form_resonance_dt, dt_sum_res)
      call gmSetValueSetting( Form_resonance_i0, I_0)
      call gmSetValueSetting( Form_resonance_psi0, psi_0)
  
  
  
imur=(/Form_resonance_mr1,Form_resonance_mr2/)
idmur=(/Form_resonance_dmr1,Form_resonance_dmr2,Form_resonance_dmr3/)
!ijsol=
iA_str=Form_resonance_a
iphi_str=Form_resonance_phi
isum_res_val=Form_resonance_sum
ij_perp= Form_resonance_J
ijsol(1)= Form_resonance_jsol1
ijsol(2)= Form_resonance_jsol2
iI_0= Form_resonance_i0
ipsi_0=Form_resonance_psi0
  
  
     call gmSetValueSetting( Controls_angle_hiromi1 ,angle_hiromi(1)/twopi)
     call gmSetValueSetting( Controls_angle_hiromi3 ,angle_hiromi(3)/twopi)
     call gmSetValueSetting( Controls_angle_hiromi ,angle_hiromi(2)/twopi)
a_gino=0.d0
ent_gino=global_frame
  
!ent_gino(1,1)=cos(angle_hiromi)
!ent_gino(3,3)=cos(angle_hiromi)
!ent_gino(1,3)=sin(angle_hiromi)
!ent_gino(3,1)=-sin(angle_hiromi)
call geo_rot(ent_gino,angle_hiromi,1)
  
call gmSetGaugeValue( Controls_angx,0 )
call gmSetGaugeValue( Controls_angy,0 )
call gmSetGaugeValue( Controls_angz,0 )
  
      this_plot=4
      call e_gm_rescale2d_universe(plot(this_plot),m_u)
glob=global_frame
  
  
  
! Start management
   call gmManage
  
! Action loop
   do
      callid=gmAction(callid)
  
! Start of Action Loop
    call gmEnqActionState(actlist)
  
  
  
! Code to handle callbacks
      if (callid == (m_vcontrol)) then
                     call gmSetValueSetting(widget=Controls_n_nd ,value=float(n_nd) )
               call set_ptc_states(my_estate)
               call gmDrawWindow(Controls )
      else if (callid == (myfrotran)) then
         !call fit_inj(m_u%end%previous,m_u%end%previous%previous)
  
      else if (callid == (Form1_quit)) then
                  close(mstable)
                   mstable=0
         exit
  
      else if (callid == (Form_frame_size_xmin)) then
          plot_size(this_plot)%xmin = gmEnqValueSetting(coord(1))
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           if(didlat) call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Form_frame_size_xmax)) then
         plot_size(this_plot)%xmax = gmEnqValueSetting(coord(2))
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           if(didlat) call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Form_frame_size_ymin)) then
         plot_size(this_plot)%ymin = gmEnqValueSetting(coord(3))
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           if(didlat) call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Form_frame_size_ymax)) then
         plot_size(this_plot)%ymax = gmEnqValueSetting(coord(4))
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           if(didlat) call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Form_frame_size_this_plot)) then
                  i = gmEnqValueSetting(Form_frame_size_this_plot )
         !if(i>=1.and.i<=5) then   ! may be needed
         if(i>=1) then   ! may be needed
         this_plot=i
         call Enq_coordinate_fields
               call set_coordinate_fields
         	
               call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
               call gmDrawWindow(Form_frame_size )
         endif
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
          if(didlat)  call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
      else if (callid == (Form_frame_size_colour)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(ray%colour,red, green, blue)
  
  
      else if (callid == (Form_frame_size_scale)) then
         !if(this_plot==1.or.this_plot==2) then
           call move_to(my_ering,fib,dis_pos)
                  i1=lat(2*this_plot-1,2*this_plot-1,this_plot)
                  i2=lat(2*this_plot,2*this_plot,this_plot)
  
                  x=sqrt(fib%i%beta(i2))/fib%i%beta(i1)
  
  
         plot_size(this_plot)%ymin = x*gmEnqValueSetting(coord(1))
         plot_size(this_plot)%ymax = x*gmEnqValueSetting(coord(2))
         !
  
         call gmSetValueSetting(coord(3),plot_size(this_plot)%ymin)
         call gmSetValueSetting(coord(4),plot_size(this_plot)%ymax)
         if(this_plot<=3) then
         ! plot_size(this_plot)%ymin = plot_size_sig(this_plot)%ymin *sig_plot(2*this_plot)
         ! plot_size(this_plot)%ymax = plot_size_sig(this_plot)%ymax *sig_plot(2*this_plot)
  
         ! normalized
          plot_size(5+this_plot)%xmin = sqrt(x)*plot_size(this_plot)%xmin
          plot_size(5+this_plot)%xmax = sqrt(x)*plot_size(this_plot)%xmax
          plot_size(5+this_plot)%ymin = plot_size(5+this_plot)%xmin
          plot_size(5+this_plot)%ymax = plot_size(5+this_plot)%xmax
         endif
  
  
  
         call set_coordinate_fields
               call gmSetGraphicsFrameBounds(plot(5+this_plot),plot_size(5+this_plot))
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
         !      call gmRemoveWindow( Form_frame_size)
         !endif
  
      else if (callid == (Form_frame_size_double)) then
         x2=2.0
  
          plot_size(this_plot)%xmin=x2*gmEnqValueSetting(coord(1))
          plot_size(this_plot)%xmax=x2*gmEnqValueSetting(coord(2))
          plot_size(this_plot)%ymin =x2*gmEnqValueSetting(coord(3))
          plot_size(this_plot)%ymax =x2*gmEnqValueSetting(coord(4))
  
  
         call set_coordinate_fields
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
  
      else if (callid == (Form_frame_size_half)) then
         x2=0.5
  
          plot_size(this_plot)%xmin=x2*gmEnqValueSetting(coord(1))
          plot_size(this_plot)%xmax=x2*gmEnqValueSetting(coord(2))
          plot_size(this_plot)%ymin =x2*gmEnqValueSetting(coord(3))
          plot_size(this_plot)%ymax =x2*gmEnqValueSetting(coord(4))
  
  
         call set_coordinate_fields
               call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmDrawWindow(Form_frame_size )
  
      else if (callid == (mopennd)) then
         do i=n_nd,1,-1
                call gmDrawWindow(form_plot(i) )
         enddo
  
  
      else if (callid == (c_norm_w)) then
         do i=6,7
                call gmDrawWindow(form_plot(i) )
         enddo
      else if (callid == (Controls_Open_Linear_Graph_entry)) then
         call gmDrawWindow(form_plot(9) )
      else if (callid == (mlayoutopen)) then
                call gmDrawWindow(form_plot(4) )
  
      else if (callid == (Controls_Lattice_Function_Menu_entry)) then
                call gmDrawWindow(Form_lat )
      else if (callid == (Controls_plot_3d_open_entry)) then
                call gmDrawWindow(form_plot(8) )
  
      else if (callid == (Controls_Open_Resonance_Plot_entry)) then
         call gmDrawWindow(form_plot(10) )
      else if (callid == (c_plots)) then
         do i=tplot,1,-1
                call gmDrawWindow(form_plot(i) )
         enddo
      else if (callid == (Controls_How_and_where_to_bitch_entry)) then
          call gmDrawWindow(Form_warn)
  
      else if (callid == (Controls_Remove_Where)) then
         call kanalnummer(mwarn)
         open(unit=mwarn,file='warn.txt')
          write(mwarn,*) .false.
         close(mwarn)
         call gmRemoveWindow(Form_warn)
  
  
  
      else if (callid == (Controls_Make_Orbit_Lattice_entry)) then
                 write(6,*) " Give Lmax"
                 read(5,*) lmax
         if(.not.associated(MY_eRING%t))then
           write(6,*) "Making a node layout "
           call MAKE_NODE_LAYOUT(MY_eRING)
           call FILL_SURVEY_DATA_IN_NODE_LAYOUT(MY_eRING)
          endif
            call move_to(my_ering,fib,my_start)
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
         call warn_t(fib,Form_States_line)
  
                 if(lmax>0) then
                    CALL ORBIT_MAKE_NODE_LAYOUT(my_ering,my_true)
                 else
                    lmax=-lmax
                    CALL ORBIT_MAKE_NODE_LAYOUT(my_ering,my_false)
                 endif
         my_ORBIT_LATTICE=>my_ering%t%ORBIT_LATTICE
         my_ORBIT_LATTICE%state=>my_estate
         my_ORBIT_LATTICE%ORBIT_USE_ORBIT_UNITS=.false.
                 write(6,*) " $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
                 write(6,*) " ORBIT_USE_ORBIT_UNITS ",my_ORBIT_LATTICE%ORBIT_USE_ORBIT_UNITS
                 write(6,*) " ORBIT_N_NODE ",my_ORBIT_LATTICE%ORBIT_N_NODE
                 write(6,*) " ORBIT_WARNING ",my_ORBIT_LATTICE%ORBIT_WARNING
                 write(6,*) " ORBIT_OMEGA ",my_ORBIT_LATTICE%ORBIT_OMEGA
                 write(6,*) " ORBIT_HARMONIC ",my_ORBIT_LATTICE%ORBIT_HARMONIC
                 write(6,*) " ORBIT_P0C ",my_ORBIT_LATTICE%ORBIT_P0C
                 write(6,*) " ORBIT_KINETIC ",my_ORBIT_LATTICE%ORBIT_KINETIC
                 write(6,*) " ORBIT_MASS_IN_AMU ",my_ORBIT_LATTICE%ORBIT_MASS_IN_AMU
                 write(6,*) " ORBIT_L ",my_ORBIT_LATTICE%ORBIT_L
                 write(6,*) " ORBIT_GAMMAT ",my_ORBIT_LATTICE%ORBIT_GAMMAT
                 write(6,*) " ORBIT_CHARGE ",my_ORBIT_LATTICE%ORBIT_CHARGE
                 write(6,*) " ORBIT_LMAX AND LMAX ",my_ORBIT_LATTICE%ORBIT_LMAX,LMAX
                 write(6,*) " $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
  
               ! CALL de_Set_Up_ORBIT_LATTICE(my_ering%t%ORBIT_LATTICE)
  
               ! nullify(my_ering%t%ORBIT_LATTICE)
               ! WRITE(6,*) " ORBIT LATTICE HAS BEEN KILLED "
  
      else if (callid == (Controls_Exit_Graphic_Interface_entry)) then
                  close(mstable)
                   mstable=0
         exit
  
      else if (callid == (Controls_Button3+GIDENT)) then
         call gmClearGraphicsFrame(plot(this_plot))
      else if (callid == (Controls_coord1)) then
         call Enq_coordinate_fields
               call set_coordinate_fields
         	
               call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
               call gmDrawWindow(Form_frame_size )
                  call gmSetValueSetting(Form_frame_size_this_plot , this_plot )
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           if(didlat) call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
      else if (callid == (Controls_all+GIDENT)) then
         do i=1,tplot
          call gmClearGraphicsFrame(plot(i))
         enddo
      else if (callid == (Controls_states)) then
               call gmSetValueSetting( Form_States_my_order, my_order)
               call gmSetValueSetting( Form_States_start, my_start)
               call gmSetValueSetting( Form_States_nturn, n_turns)
               call gmSetValueSetting( Form_States_my_delta, my_delta)
               call gmSetValueSetting( Form_States_apx, x_ap)
               call gmSetValueSetting( Form_States_apy, y_ap)
               call gmSetTextSetting(Form_States_name_ap ,name_ap )
               call gmSetTextSetting(Form_States_filename_ap ,filename_ap )
  
               call set_ptc_states(my_estate)
               call gmDrawWindow(Form_States )
      else if (callid == (Controls_Button12)) then
               call gmDrawWindow(Form_orbit )
      else if (callid == (Controls_n_nd)) then
               n_nd = gmEnqValueSetting(Controls_n_nd )
  
      else if (callid == (Controls_n_nd+GIDENT)) then
               n_nd = gmEnqValueSetting(Controls_n_nd )
  
      else if (callid == (Controls_MAP)) then
         CALL compute_one_turn(near,far,kob,kob0,fix3,turns )
         do i=1,3
          call e_gm_draw_cross(gred,plot(1),0.02,near(1,i),near(2,i))
         enddo
         do i=1,3
          call e_gm_draw_cross(gblue,plot(1),0.02,far(1,i),far(2,i))
         enddo
         do i=1,3
          call e_gm_draw_cross(ggreen,plot(1),0.02,kob(1,i),kob(2,i))
         enddo
         do i=1,3
          call e_gm_draw_cross(gyellow,plot(1),0.02,kob0(1,i),kob0(2,i))
         enddo
          call e_gm_draw_cross(gcyan,plot(1),0.02,real(fix3(1)),real(fix3(2)))
  
               call gmSetValueSetting(Controls_turns ,turns )
      else if (callid == (Controls_mis)) then
         call gmDrawWindow(Form_mis )
         call set_sig_mis
      else if (callid == (Controls_bump)) then
         call scale_fam(my_ering,1)
         call gmSetWidgetProp( Controls_bump,gmDisplayState=ghidden)
  
      else if (callid == (Controls_Button18)) then
         turns=1
         call compute_lattice_function(ai0)
         call gmSetWidgetStatus(M_select, GSELECTABLE)
  
  
         call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
           call move_to(my_ering,fib,dis_pos)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
           call gmSetValueSetting(Form_lat_s ,fib%i%s)
  
  
  
                 i_lat=gmEnqValueSetting(Form_lat_i )
                 n_lat=lat(i_lat,j_lat,k_lat)
         call gmEnqWidgetInfo(M_plotbetax,gwid)
         gwid%label="Plot "//lat_name(n_lat)	
         call gmSetWidgetInfo(M_plotbetax,gwid)
         call gmEnqWidgetInfo(m_scalebetax,gwid)
         gwid%label="Scale for "//lat_name(n_lat)	
         call gmSetWidgetInfo(m_scalebetax,gwid)
  
               call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmSetWidgetStatus(m_auto_scale, GSELECTABLE)   ! hum....
         call gmDrawWindow(Form_lat )
         didlat=.true.
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Controls_Button20)) then
               call gmDrawWindow(Form_dyn )
      else if (callid == (Controls_lat_phase_tune)) then
         call compute_lattice_function
         call compute_nonlinear_tune(q,dtune)
           call move_to(my_ering,fib,dis_pos)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
           call gmSetValueSetting(Form_lat_s ,fib%i%s)
  
               call gmSetValueSetting(Form_lat_qx ,q(1,0) )
               call gmSetValueSetting(Form_lat_qy ,q(2,0) )
               call gmSetValueSetting(Form_lat_qxp ,q(1,1) )
               call gmSetValueSetting(Form_lat_qyp ,q(2,1) )
               call gmSetValueSetting(Form_lat_qxpp ,q(1,2) )
               call gmSetValueSetting(Form_lat_qypp ,q(2,2) )
               call gmSetValueSetting(Form_lat_d11 ,dtune(1))
               call gmSetValueSetting(Form_lat_d22 ,dtune(2))
               call gmSetValueSetting(Form_lat_d12 ,dtune(3))
  
               call gmDrawWindow(Form_lat )
         call gmSetWidgetStatus(M_select, GSELECTABLE)
         call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
  
         didlat=.true.
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Controls_turns)) then
               turns= gmEnqValueSetting(Controls_turns )
      else if (callid == (Controls_Button23)) then
         call compute_only_map("only_map.txt")
      else if (callid == (Controls_flag_layout)) then
               flag_layout= gmEnqValueSetting(Controls_flag_layout )
               flag_layout_default=flag_layout
      else if (callid == (Controls_layout_number)) then
               i_layout= gmEnqValueSetting(Controls_layout_number )
               if(i_layout>m_u%n) then
                write(6,*) " Universe Size ", m_u%n
                i_layout=m_u%n
                call gmSetValueSetting(Controls_layout_number,i_layout )
  
               endif
         !      call move_to_layout_i(m_u,my_ering,m_u%shared+i_layout)
                call move_to_layout_i(m_u,my_ering,i_layout)
  
                write(6,*) my_ering%name(1:len_trim(my_ering%name))
  
         my_start=1
  
         call move_to(my_ering,fib,my_start)
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
         call warn_t(fib,Form_States_line)
  
  
               call gmEnqWidgetInfo(Controls_m_t_number, prop_widget)
                prop_widget%colour1=col_off
               call gmSetWidgetInfo(Controls_m_t_number, prop_widget)
               call gmEnqWidgetInfo(Controls_layout_number, prop_widget)
                prop_widget%colour1=col_on
               call gmSetWidgetInfo(Controls_layout_number, prop_widget)
  
  
      else if (callid == (Controls_Button46)) then
         call survey(my_ering)
         WRITE(6,*) " Survey is Done "
  
      else if (callid == (Controls_scriptfilename)) then
                  call gmEnqTextSetting(Controls_scriptfilename ,scriptfilename )
                  call context(scriptfilename)
  
      else if (callid == (Controls_scriptfilename+7*GIDENT)) then
  
         scriptfilename=actlist%string
                  call gmSetTextSetting(Controls_scriptfilename,scriptfilename)
      else if (callid == (Controls_Button54)) then
         call charge_dir(my_ering)
      else if (callid == (Controls_Button52)) then
         TEMP_STATE=MY_eSTATE
             call kanalnummer(i)
              open(unit=i,file=scriptfilename,status='OLD',err=4002)
              goto 4001
              4002 continue
              close(i)
                     call gmSetTextSetting(Form_warn_gauge_Text52 , "File "// &
          scriptfilename(1:len_trim(scriptfilename))//" does not exist" )
                     call gmDrawWindow(Form_warn_gauge)
              goto 4003
              4001 continue
             close(i)
  
             CALL read_ptc_command77(scriptfilename)
  
         4003 continue
         MY_eSTATE=TEMP_STATE
      else if (callid == (Controls_flatin)) then
                  call gmEnqTextSetting(Controls_flatin ,fileflatold)
         call context(fileflatold)
  
      else if (callid == (Controls_flatin+7*GIDENT)) then
         fileflatold=actlist%string
                  call gmSetTextSetting(Controls_flatin ,fileflatold)
  
  
      else if (callid == (Controls_flatout)) then
                  call gmEnqTextSetting(Controls_flatout ,fileflatnew)
         call context(fileflatnew)
  
      else if (callid == (Controls_flatout+7*GIDENT)) then
         fileflatnew=actlist%string
                  call gmSetTextSetting(Controls_FLATOUT,fileflatnew)
  
      else if (callid == (Controls_Button59)) then
         if(new_flat) then
          call print_new_flat(MY_eRING,fileflatnew)
         else
          call print_COMPLEX_SINGLE_STRUCTURE(MY_eRING,fileflatnew)
         endif
  
  
  
      else if (callid == (Controls_track_file)) then
                  call gmEnqTextSetting(Controls_track_file ,trackfile)
         call context(trackfile)
  
      else if (callid == (Controls_track_file+7*GIDENT)) then
  
         trackfile=actlist%string
                  call gmSetTextSetting(Controls_track_file,trackfile)
  
      else if (callid == (Controls_Button66)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(ray%colour,red, green, blue)
  
  
      else if (callid == (Controls_Button67)) then
         i=frasav(plot(4),"junk.bmp")
         write(6,*) "saved flag =",i
      else if (callid == (Controls_Button69)) then
         call gmDefineTimerCallback(time_accel,track_accel)
  
  
  
      else if (callid == (Controls_Button71)) then
         call gmDefineTimerCallback(0,track_accel)
          call gmDefineTimerCallback(0,track_plot)
          iplot=0
  
  
  
  
      else if (callid == (Controls_ncode)) then
                 ncode  = gmEnqValueSetting(Controls_ncode )
      else if (callid == (Controls_Button72)) then
         call gmDefineTimerCallback(time_plot,track_plot)
         iplot=0
      else if (callid == (Controls_kplot)) then
                 kplot = gmEnqValueSetting(Controls_kplot )
      else if (callid == (Controls_Button58)) then
             call kanalnummer(i)
              open(unit=i,file=fileflatold,status='OLD',err=3002)
              goto 3001
              3002 continue
              close(i)
                     call gmSetTextSetting(Form_warn_gauge_Text52 , "File "// &
          fileflatold(1:len_trim(fileflatold))//" does not exist" )
                     call gmDrawWindow(Form_warn_gauge)
              goto 3003
              3001 continue
             close(i)
         i=m_u%n
         if(new_flat) then
          call read_lattice_append(M_U,fileflatold)
         else
          CALL  READ_AND_APPEND_VIRGIN_general(M_U,fileflatold)
         endif
         if(m_u%n>i) then
               i_layout= m_u%n
                call move_to_layout_i(m_u,my_ering,i_layout)
                call gmSetValueSetting(Controls_layout_number,i_layout )
  
                write(6,*) my_ering%name(1:len_trim(my_ering%name))
         my_start=1
  
         call move_to(my_ering,fib,my_start)
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
         call warn_t(fib,Form_States_line)
  
  
           CALL gmSETValueSetting(Form_States_start, my_start )
            dis_pos=my_start
            call move_to(my_ering,fib,my_start)
           call gmSetTextSetting(Form_states_name ,fib%mag%name)
         endif
  
         3003 continue
  
  
  
      else if (callid == (Controls_Button70)) then
         xsync=zero
  
         !write(6,*) "method 1 or 2 "
         !read(5,*) accel_method
         !write(6,*) "number of rays per ring, # ring, # delta, number of turns"
         !read(5,*) nperring,i1,ndelta, nturn_dejan
         write(6,*) "number of rays per ring, # ring, # delta"
         read(5,*) nperring,i1,ndelta
         icode=0
         ifile=0
         nturn_dejan=1000000
         !i=-i
         !subroutine  set_cavity_CancerTherapy(L,VOLT,nt,nrays,nr,ndelta)
          ! nrays is per ring, nr # of ring
         call set_distribution_for_beam(my_ering,nperring,i1,ndelta)
         k_cav_plot=NCAV_DEJAN
         write(6,*) k_cav_plot
         call gmSetValueSetting(Controls_cav_per_plot ,k_cav_plot )
  
      else if (callid == (Controls_Combo_script)) then
                  call gmEnqTextSetting( Controls_Combo_script,combo_entry)
                  scriptfilename =combo_entry
                  call context(scriptfilename)
                  call gmSetTextSetting(Controls_scriptfilename ,scriptfilename )
  
      else if (callid == (Controls_Button80)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_layout,red, green, blue)
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_plot,red, green, blue)
  
  
      else if (callid == (Controls_cav_per_plot)) then
         k_cav_plot = gmEnqValueSetting(Controls_cav_per_plot )
      else if (callid == (Controls_Button85)) then
         call plot_beam
      else if (callid == (Controls_survey_DNA)) then
         !      call gmClearGraphicsFrame(plot(this_plot))
         if(m_t%n/=0) then
  
         l_dummy=>m_t%start
  
               do i1=1,m_t%n
                 call survey(l_dummy)
                 l_dummy=>l_dummy%next
               enddo
         endif
  
         WRITE(6,*) " Survey of all trackables is done "
      else if (callid == (Controls_Toggle_random_colour)) then
         if(gmEnqToggleSwitch_et(state(15))) then
          random_colour=.true.
         else
          random_colour=.false.
         endif
  
      else if (callid == (Controls_Button87)) then
         turns=1
         call compute_lattice_function(ait,.false.)
         call gmSetWidgetStatus(M_select, GSELECTABLE)
  
  
         call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
           call move_to(my_ering,fib,dis_pos)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
           call gmSetValueSetting(Form_lat_s ,fib%i%s)
  
  
  
                 i_lat=gmEnqValueSetting(Form_lat_i )
                 n_lat=lat(i_lat,j_lat,k_lat)
         call gmEnqWidgetInfo(M_plotbetax,gwid)
         gwid%label="Plot "//lat_name(n_lat)	
         call gmSetWidgetInfo(M_plotbetax,gwid)
         call gmEnqWidgetInfo(m_scalebetax,gwid)
         gwid%label="Scale for "//lat_name(n_lat)	
         call gmSetWidgetInfo(m_scalebetax,gwid)
  
               call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmDrawWindow(Form_lat )
         i=this_plot
         if(i>=4) then
         call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
  
         else
           call gmSetWidgetStatus(Form_frame_size_scale, GSELECTABLE)
          if(i==3.and.c_%ndpt/=0) call gmSetWidgetStatus(Form_frame_size_scale, GunSELECTABLE)
         endif
  
  
      else if (callid == (Controls_Buttontwiss)) then
               call gmDrawWindow(Form30_twiss)
  
      else if (callid == (Controls_Button101+GIDENT)) then
         write(6,*) "c_%no,c_%nv,c_%nd,c_%nd2"
         write(6,*) c_%no,c_%nv,c_%nd,c_%nd2
         write(6,*) "c_%ndpt,c_%npara,c_%npara,c_%np_pol"
         write(6,*)  c_%ndpt,c_%npara,c_%npara,c_%np_pol
  
  
  
      else if (callid == (Controls_Button119)) then
         call gmDrawWindow(Form_lat )
  
      else if (callid == (Controls_orbit)) then
         call read_ptc_states(my_estate)
         write(6,*) "orbit_track",orbit_track
         write(6,*) "Accelerate",accelerate
         if(associated(my_ering%t)) then
          if(associated(my_ering%t%ORBIT_LATTICE)) then
           write(6,*) "Orbit Units ",my_ering%t%ORBIT_LATTICE%ORBIT_USE_ORBIT_UNITS
           CALL PRINT(my_ering%t%ORBIT_LATTICE%state,6)
          endif
         endif
  
  
      else if (callid == (Controls_angx)) then
  
             angx = nint(gmEnqGaugeValue(Controls_angx )+0.1)
                   ang_gino=0.d0
                   ang_gino(1)=angx*twopi/100.d0
                   write(6,*) " ang in x = ",angx/100.d0,angx/100.d0*360
  
  
  
  
         call gmSetGaugeValue( Controls_angy,0 )
         call gmSetGaugeValue( Controls_angz,0 )
  
      else if (callid == (Controls_angy)) then
  
             angy = nint(gmEnqGaugeValue(Controls_angy )+0.1)
                   ang_gino=0.d0
                   ang_gino(2)=angy*twopi/100.d0
                   write(6,*) " ang in y = ",angy/100.d0,angy/100.d0*360
  
  
         call gmSetGaugeValue( Controls_angx,0 )
  
         call gmSetGaugeValue( Controls_angz,0 )
  
      else if (callid == (Controls_angz)) then
  
             angz = nint(gmEnqGaugeValue(Controls_angz )+0.1)
                   ang_gino=0.d0
                   ang_gino(3)=angz*twopi/100.d0
                   write(6,*) " ang in z = ",angz/100.d0,angz/100.d0*360
  
  
  
         call gmSetGaugeValue( Controls_angx,0 )
         call gmSetGaugeValue( Controls_angy,0 )
  
  
      else if (callid == (Controls_centre_layout+GIDENT)) then
         a_gino=0.d0
         ent_gino=global_frame
  
         !ent_gino(1,1)=cos(angle_hiromi)
         !ent_gino(3,3)=cos(angle_hiromi)
         !ent_gino(1,3)=sin(angle_hiromi)
         !ent_gino(3,1)=-sin(angle_hiromi)
         call geo_rot(ent_gino,angle_hiromi,1)
  
         call gmSetGaugeValue( Controls_angx,0 )
         call gmSetGaugeValue( Controls_angy,0 )
         call gmSetGaugeValue( Controls_angz,0 )
  
      else if (callid == (Controls_Button132)) then
          call GEO_ROT(ent_gino,ANG_gino,1,basis=ent_gino)
  
  
         l_dummy=>m_U%start
         do i=1,m_u%n
          call e_gm_draw_magnets(l_dummy,colour_magnet,plot(4))
          l_dummy=>l_dummy%next
         enddo
         call e_gm_draw_drum(plot(4),0.0d0,0.0d0,r_hiromi,h_hiromi)
  
  
      else if (callid == (Controls_Button133)) then
         call e_gm_magnet_centre(my_ering)
  
      else if (callid == (Controls_Button134+GIDENT)) then
         call e_gm_draw_hiromi(1,plot(4))
  
      else if (callid == (Controls_Button134+6*GIDENT)) then
         call e_gm_draw_hiromi(0,plot(4))
  
      else if (callid == (Controls_Button135)) then
         call e_gm_draw_drum(plot(4),0.0d0,0.0d0,r_hiromi,h_hiromi)
  
  
      else if (callid == (Controls_Button136)) then
         call  e_gm_track_hiromi(ray,plot(4))
  
      else if (callid == (Controls_angle_hiromi)) then
         angle_hiromi(2)= twopi*gmEnqValueSetting(Controls_angle_hiromi )
      else if (callid == (Controls_Button137)) then
         call e_gm_draw_hiromi(0,plot(4))
      else if (callid == (Controls_Button60)) then
                call gmDrawWindow(form_plot(4) )
  
  
      else if (callid == (Controls_angle_hiromi3)) then
         angle_hiromi(3)= twopi*gmEnqValueSetting(Controls_angle_hiromi3 )
      else if (callid == (Controls_angle_hiromi1)) then
         angle_hiromi(1)= twopi*gmEnqValueSetting(Controls_angle_hiromi1 )
      else if (callid == (Controls_Toggle28)) then
         call read_ptc_states(my_estate)
  
      else if (callid == (Controls_dotted)) then
         if(gmEnqToggleSwitch_et(Controls_dotted)) then
          dirdotted=.true.
         else
          dirdotted=.false.
         endif
      else if (callid == (Controls_Button147+GIDENT)) then
          WRITE(6,*) " BEGINNING OF MAGNET"
          WRITE(6,*) FIB%MAG%P%F%A
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%MAG%P%F%ENT(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%MAG%P%F%ENT(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%MAG%P%F%ENT(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob, FIB%MAG%P%F%ENT,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%mag%p%F%A,FIB%mag%p%F%ENT)
  
         a_gino=FIB%mag%p%F%A
         ent_gino=FIB%mag%p%F%ENT
  
      else if (callid == (Controls_Button148+GIDENT)) then
  
           WRITE(6,*) " MIDDLE OF FIBRE "
           WRITE(6,*) FIB%CHART%F%o
           WRITE(6,*) " VEC X "
           WRITE(6,*) FIB%CHART%F%mid(1,:)
           WRITE(6,*) " VEC Y "
           WRITE(6,*) FIB%CHART%F%mid(2,:)
           WRITE(6,*) " VEC Z "
           WRITE(6,*) FIB%CHART%F%mid(3,:)
  
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob,FIB%CHART%F%mid,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
  
         call e_gm_draw_frame(plot(4),FIB%CHART%F%O,FIB%CHART%F%MID)
  
         a_gino=FIB%CHART%F%O
         ent_gino=FIB%CHART%F%MID
  
      else if (callid == (Controls_Button149+GIDENT)) then
          WRITE(6,*) " END OF FIBRE "
          WRITE(6,*) FIB%CHART%F%b
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%CHART%F%exi(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%CHART%F%exi(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%CHART%F%exi(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob,FIB%CHART%F%exi,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%CHART%F%B,FIB%CHART%F%EXI)
  
         a_gino=FIB%CHART%F%B
         ent_gino=FIB%CHART%F%EXI
  
      else if (callid == (Controls_Button150+GIDENT)) then
          WRITE(6,*) " MIDDLE OF  MAGNET"
          WRITE(6,*) FIB%MAG%P%F%O
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%MAG%P%F%mid(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%MAG%P%F%mid(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%MAG%P%F%mid(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob, FIB%MAG%P%F%mid,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%mag%p%F%O,FIB%mag%p%F%mid)
  
         a_gino=FIB%mag%p%F%O
         ent_gino=FIB%mag%p%F%mid
  
      else if (callid == (Controls_Button151+GIDENT)) then
          WRITE(6,*) " END OF  MAGNET"
          WRITE(6,*) FIB%MAG%P%F%B
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%MAG%P%F%EXI(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%MAG%P%F%EXI(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%MAG%P%F%EXI(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob, FIB%MAG%P%F%exi,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
  
         call e_gm_draw_frame(plot(4),FIB%mag%p%F%B,FIB%mag%p%F%EXI)
  
         a_gino=FIB%mag%p%F%B
         ent_gino=FIB%mag%p%F%EXI
  
      else if (callid == (Controls_Button146+GIDENT)) then
          WRITE(6,*) " BEGINNING OF FIBRE "
          WRITE(6,*) FIB%CHART%F%A
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%CHART%F%ENT(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%CHART%F%ENT(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%CHART%F%ENT(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob,FIB%CHART%F%ENT,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%CHART%F%A,FIB%CHART%F%ENT)
  
         a_gino=FIB%CHART%F%A
         ent_gino=FIB%CHART%F%ENT
  
      else if (callid == (Controls_Button152+GIDENT)) then
          WRITE(6,*) " BEGINNING OF MAGNET"
          WRITE(6,*) FIB%MAG%P%F%A
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%MAG%P%F%ENT(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%MAG%P%F%ENT(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%MAG%P%F%ENT(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob, FIB%MAG%P%F%ENT,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%mag%p%F%A,FIB%mag%p%F%ENT)
  
  
  
      else if (callid == (Controls_Button153+GIDENT)) then
          WRITE(6,*) " BEGINNING OF FIBRE "
          WRITE(6,*) FIB%CHART%F%A
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%CHART%F%ENT(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%CHART%F%ENT(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%CHART%F%ENT(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob,FIB%CHART%F%ENT,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%CHART%F%A,FIB%CHART%F%ENT)
  
  
  
      else if (callid == (Controls_Button154+GIDENT)) then
  
           WRITE(6,*) " MIDDLE OF FIBRE "
           WRITE(6,*) FIB%CHART%F%o
           WRITE(6,*) " VEC X "
           WRITE(6,*) FIB%CHART%F%mid(1,:)
           WRITE(6,*) " VEC Y "
           WRITE(6,*) FIB%CHART%F%mid(2,:)
           WRITE(6,*) " VEC Z "
           WRITE(6,*) FIB%CHART%F%mid(3,:)
  
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob,FIB%CHART%F%mid,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
  
         call e_gm_draw_frame(plot(4),FIB%CHART%F%O,FIB%CHART%F%MID)
  
  
  
      else if (callid == (Controls_Button155+GIDENT)) then
          WRITE(6,*) " MIDDLE OF  MAGNET"
          WRITE(6,*) FIB%MAG%P%F%O
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%MAG%P%F%mid(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%MAG%P%F%mid(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%MAG%P%F%mid(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob, FIB%MAG%P%F%mid,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%mag%p%F%O,FIB%mag%p%F%mid)
  
  
  
      else if (callid == (Controls_Button156+GIDENT)) then
          WRITE(6,*) " END OF FIBRE "
          WRITE(6,*) FIB%CHART%F%b
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%CHART%F%exi(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%CHART%F%exi(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%CHART%F%exi(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob,FIB%CHART%F%exi,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
         call e_gm_draw_frame(plot(4),FIB%CHART%F%B,FIB%CHART%F%EXI)
  
  
  
      else if (callid == (Controls_Button157+GIDENT)) then
          WRITE(6,*) " END OF  MAGNET"
          WRITE(6,*) FIB%MAG%P%F%B
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%MAG%P%F%EXI(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%MAG%P%F%EXI(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%MAG%P%F%EXI(3,:)
  
         call COMPUTE_ENTRANCE_ANGLE_bmad(glob, FIB%MAG%P%F%exi,ANG)
         Write(6,'(a13,(1x,f12.5),a6,(1x,f12.5),a6,(1x,f12.5))') " BMAD theta =",ang(2)," phi =",ang(1)," psi =",ang(3)
  
         call e_gm_draw_frame(plot(4),FIB%mag%p%F%B,FIB%mag%p%F%EXI)
  
  
  
      else if (callid == (Controls_m_t_number)) then
               i_layout_t= gmEnqValueSetting(Controls_m_t_number )
               if(i_layout_t>m_t%n) then
                write(6,*) " Universe Size ", m_t%n
                i_layout_t=m_t%n
                call gmSetValueSetting(Controls_m_t_number,i_layout_t )
  
               endif
         !      call move_to_layout_i(m_u,my_ering,m_u%shared+i_layout)
         if(m_t%n/=0) then
                call move_to_layout_i(m_t,my_ering,i_layout_t)
  
                write(6,*) my_ering%name(1:len_trim(my_ering%name))
  
         my_start=1
  
         call move_to(my_ering,fib,my_start)
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
         call warn_t(fib,Form_States_line)
  
  
               call gmEnqWidgetInfo(Controls_m_t_number, prop_widget)
                prop_widget%colour1=col_on
               call gmSetWidgetInfo(Controls_m_t_number, prop_widget)
               call gmEnqWidgetInfo(Controls_layout_number, prop_widget)
                prop_widget%colour1=col_off
               call gmSetWidgetInfo(Controls_layout_number, prop_widget)
  
  
  
         endif
  
  
      else if (callid == (Controls_new_flat)) then
         call read_ptc_states(my_estate)
  
      else if (callid == (Controls_Button139)) then
         !      call gmClearGraphicsFrame(plot(this_plot))
  
  
         l_dummy=>m_u%start
  
               do i1=1,m_u%n
                 call survey(l_dummy)
                 l_dummy=>l_dummy%next
               enddo
  
         WRITE(6,*) " Survey of data base is done "
  
      else if (callid == (Controls_Button140+GIDENT)) then
         call read_ptc_command("screen")
  
      else if (callid == (Controls_plot_e_beam+GIDENT)) then
         do i=1,n_nd
         if(around_fix) then
          call egm_scale_draw_e_beam(my_e_beam,i,my_FIX)
         else
          call egm_scale_draw_e_beam(my_e_beam,i)
         endif
         enddo
  
      else if (callid == (Controls_size_e_beam+GIDENT)) then
         do i=1,n_nd
         if(around_fix) then
          call egm_scale_plot_e_beam(my_e_beam,i,my_FIX)
         else
          call egm_scale_plot_e_beam(my_e_beam,i)
         endif
         enddo
  
         call Enq_coordinate_fields
               call set_coordinate_fields
         	if(this_plot>=1.and.this_plot<=3) then
                 call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
                 call gmDrawWindow(Form_frame_size )
                  call gmSetValueSetting(Form_frame_size_this_plot , this_plot )
               endif
  
      else if (callid == (Controls_track_e+GIDENT)) then
  
         do i=1,n_turns
         !fanglei  call track_my_beam(my_ering,my_e_Beam,my_estate,my_start)
         enddo
      else if (callid == (Controls_stop_lin+GIDENT)) then
         call gmDefineTimerCallback(0,track_lin)
         write(6,*) ilin, " turns tracked at position = ",my_start
      else if (callid == (Controls_start_lin+GIDENT)) then
         call gmDefineTimerCallback(time_lin,track_lin)
      else if (callid == (Controls_n_mul)) then
               n_mul = gmEnqValueSetting(Controls_n_mul )
  
      else if (callid == (Controls_n_mul+GIDENT)) then
               n_nd = gmEnqValueSetting(Controls_n_nd )
  
      else if (callid == (x1_x2+GIDENT)) then
         this_plot=1
  
      else if (callid == (x1_x2_Open_Help_entry)) then
               call gmDrawWindow(Form_explain )
      else if (callid == (x1_x2_Copy_to_Clipboard_entry)) then
         call gmCopyGraphicsToClipboard(x1_x2_Graphics1 ,Gcopy )
  
      else if (callid == (x1_x2_Graphics1+GIDENT)) then
         this_plot=1
         if(.not.gEnqKeyState_et(ichar("C"))) then
          x_orbit_sync(6)=xsm0t
          xsmt=xsm0t
         endif
             IF(gEnqKeyState_et(ichar("B"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
               call gmClearGraphicsFrame(plot(this_plot))
             elseif(gEnqKeyState_et(ichar("Q"))) then
                   dx(1)=actlist%x
                   dy(1)=actlist%y
                   write(6,*) dx(1),dy(1)
             elseif(gEnqKeyState_et(ichar("W"))) then
                   dx(2)=actlist%x
                   dy(2)=actlist%y
                   write(6,*) dx(2),dy(2)
                    dl0=sqrt((dx(2)-dx(1))**2+(dy(2)-dy(1))**2)
                    write(6,*) "Surface ",dl*dl0
                    dl=dl0
             elseif(gEnqKeyState_et(ichar("1"))) then
              write(6,*) " 1st boundary "
                   limit_temp%xmin=actlist%x
                   limit_temp%ymin=actlist%y
             elseif(gEnqKeyState_et(ichar("2"))) then
              write(6,*) " 2nd boundary "
                   limit_temp%xmax=actlist%x
                   limit_temp%ymax=actlist%y
  
                   write(6,*) limit_temp
                   call e_g_order(limit_temp,limit_temp)
                   write(6,*) limit_temp
         !   call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
          plot_size(this_plot)%xmin = limit_temp%xmin ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%xmax = limit_temp%xmax ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%ymin = limit_temp%ymin ! /sig_plot(2*this_plot)
          plot_size(this_plot)%ymax = limit_temp%ymax ! /sig_plot(2*this_plot)
         ! plot_size(this_plot)%xmin= plot_size_sig(this_plot)%xmin *sig_plot(2*this_plot-1)
         ! plot_size(this_plot)%xmax= plot_size_sig(this_plot)%xmax *sig_plot(2*this_plot-1)
         ! plot_size(this_plot)%ymin= plot_size_sig(this_plot)%ymin *sig_plot(2*this_plot)
         ! plot_size(this_plot)%ymax= plot_size_sig(this_plot)%ymax *sig_plot(2*this_plot)
         call set_coordinate_fields
         call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
  
             else
                IF(gEnqKeyState_et(-1)) THEN
  
         !        write(6,*) actlist%x,actlist%y
                 ray%ND=n_ND
         !        IF(.not.gEnqKeyState_et(ichar("P"))) ray%r=0.0_dp
                 ray%NTURNS=n_turns
                 ray%plane=this_plot
                 if(gEnqKeyState_et(ichar("F"))) then
                  actlist%x=0.d0;actlist%y=0.d0;
                 endif
                 IF(gEnqKeyState_et(ichar("I"))) then
                 Write(6,*) " Input x and px "
                  read(5,*) actlist%x,actlist%y
                 endif
                 IF(gEnqKeyState_et(ichar("R"))) then
             call kanalnummer(i)
              open(unit=i,file=trackfile,status='OLD',err=31002)
              goto 31001
              31002 continue
              close(i)
                     call gmSetTextSetting(Form_warn_gauge_Text52 , "File "// &
          trackfile(1:len_trim(trackfile))//" does not exist" )
                     call gmDrawWindow(Form_warn_gauge)
              goto 31003
              31001 continue
              read(i,*) nraysfile,n_ND
  
               n_ND=n_ND/2
               call gmSetValueSetting(Controls_n_nd ,n_nd )
               ray%ND=n_ND
               allocate(track_data_f(2*n_ND,nraysfile))
               do mti=1,nraysfile
                read(i,*) track_data_f(1:2*n_ND,mti)
               enddo
             close(i)
  
                 endif
                 IF(gEnqKeyState_et(ichar("P"))) then
                    ray%r=previous_ray
                    KEEP_COLOUR=.false.
                 elseif(gEnqKeyState_et(ichar("C"))) then
                    KEEP_COLOUR=.TRUE.
                  write(6,*) "Keep data in plane",this_plot
                 else
                    ray=actlist
                    previous_ray=ray%r
                 endif
                 ray%widget(this_plot)=plot(this_plot)
         !        call egm_scale_draw(plot(1),actlist%x,actlist%y,ray%colour)
                 RAY%IN(THIS_PLOT)=.TRUE.
                 if(nraysfile/=0) then
                  do mti=1,nraysfile
                   RAY%IN(1:n_nd)=.TRUE.
                   ray%widget(1:n_nd)=plot(1:n_nd)
                   ray%r=track_data_f(1:2*n_ND,mti)
                   call track_standard(ray,flag_layout,track_a,track_map,turns)
                  enddo
                 else
                  call track_standard(ray,flag_layout,track_a,track_map,turns)
                 endif
                 ray%stable=.true.
                  ray%aperture=.true.
                call gFlushGraphics
                 ENDIF
             endif
                if(nraysfile/=0) deallocate(track_data_f)
                nraysfile=0
  
  
              31003 continue
  
  
  
      else if (callid == (x1_x2_Graphics1+2*GIDENT)) then
         !this_plot=1
         !call e_g_resize_graphics(plot(this_plot),actlist,done)
         !if(done) then
         !      call Enq_coordinate_fields
         !      call set_coordinate_fields!
         	
         !      call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
         !      call gmDrawWindow(Form_frame_size )
         !endif
         !done=.false.
      else if (callid == (x3_x4+GIDENT)) then
         this_plot=2
  
      else if (callid == (x3_x4_Copy_to_Clipboard_entry)) then
         call gmCopyGraphicsToClipboard(x3_x4_Graphics2 ,Gcopy )
  
      else if (callid == (x3_x4_Graphics2+GIDENT)) then
         this_plot=2
         if(.not.gEnqKeyState_et(ichar("C"))) then
          x_orbit_sync(6)=xsm0t
          xsmt=xsm0t
         endif
             IF(gEnqKeyState_et(ichar("B"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
               call gmClearGraphicsFrame(plot(this_plot))
             elseif(gEnqKeyState_et(ichar("1"))) then
              write(6,*) " 1st boundary "
                   limit_temp%xmin=actlist%x
                   limit_temp%ymin=actlist%y
            elseif(gEnqKeyState_et(ichar("2"))) then
              write(6,*) " 2nd boundary "
                   limit_temp%xmax=actlist%x
                   limit_temp%ymax=actlist%y
  
                   write(6,*) limit_temp
                   call e_g_order(limit_temp,limit_temp)
                   write(6,*) limit_temp
         !   call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
          plot_size(this_plot)%xmin = limit_temp%xmin ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%xmax = limit_temp%xmax ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%ymin = limit_temp%ymin ! /sig_plot(2*this_plot)
          plot_size(this_plot)%ymax = limit_temp%ymax ! /sig_plot(2*this_plot)
          ! plot_size(this_plot)%xmin= plot_size_sig(this_plot)%xmin *sig_plot(2*this_plot-1)
          ! plot_size(this_plot)%xmax= plot_size_sig(this_plot)%xmax *sig_plot(2*this_plot-1)
          ! plot_size(this_plot)%ymin= plot_size_sig(this_plot)%ymin *sig_plot(2*this_plot)
          ! plot_size(this_plot)%ymax= plot_size_sig(this_plot)%ymax *sig_plot(2*this_plot)
         call set_coordinate_fields
         call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
             else
                IF(gEnqKeyState_et(-1)) THEN
  
         !        write(6,*) actlist%x,actlist%y
                 ray%ND=n_ND
         !        IF(.not.gEnqKeyState_et(ichar("P"))) ray%r=0.0_dp
                 ray%NTURNS=n_turns
                 ray%plane=this_plot
                 if(gEnqKeyState_et(ichar("F"))) then
                  actlist%x=0.d0;actlist%y=0.d0;
                 endif
                 IF(gEnqKeyState_et(ichar("I"))) then
                 Write(6,*) " Input y and py "
                  read(5,*) actlist%x,actlist%y
                 endif
                 IF(gEnqKeyState_et(ichar("P"))) then
                    KEEP_COLOUR=.false.
                    ray%r=previous_ray
                 elseif(gEnqKeyState_et(ichar("C"))) then
                    KEEP_COLOUR=.TRUE.
                  write(6,*) "Keep data in plane",this_plot
                 else
                    ray=actlist
                    previous_ray=ray%r
                 endif
                 ray%widget(this_plot)=plot(this_plot)
                 RAY%IN(THIS_PLOT)=.TRUE.
  
                 call track_standard(ray,flag_layout,track_a,track_map,turns)
                  ray%stable=.true.
                  ray%aperture=.true.
                call gFlushGraphics
                ENDIF
             endif
      else if (callid == (x5_x6+GIDENT)) then
         this_plot=3
      else if (callid == (x5_x6_Copy_to_Clipboard_entry)) then
                  call gmCopyGraphicsToClipboard(x5_x6_Graphics3 ,GCOPY )
  
      else if (callid == (x5_x6_Graphics3+GIDENT)) then
         this_plot=3
         if(.not.gEnqKeyState_et(ichar("C"))) then
          x_orbit_sync(6)=xsm0t
          xsmt=xsm0t
         endif
             IF(gEnqKeyState_et(ichar("B"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
               call gmClearGraphicsFrame(plot(this_plot))
             elseif(gEnqKeyState_et(ichar("1"))) then
              write(6,*) " 1st boundary "
                   limit_temp%xmin=actlist%x
                   limit_temp%ymin=actlist%y
            elseif(gEnqKeyState_et(ichar("2"))) then
              write(6,*) " 2nd boundary "
                   limit_temp%xmax=actlist%x
                   limit_temp%ymax=actlist%y
  
                   write(6,*) limit_temp
                   call e_g_order(limit_temp,limit_temp)
                   write(6,*) limit_temp
         !   call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
          plot_size(this_plot)%xmin = limit_temp%xmin ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%xmax = limit_temp%xmax ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%ymin = limit_temp%ymin ! /sig_plot(2*this_plot)
          plot_size(this_plot)%ymax = limit_temp%ymax ! /sig_plot(2*this_plot)
          ! plot_size(this_plot)%xmin= plot_size_sig(this_plot)%xmin *sig_plot(2*this_plot-1)
          ! plot_size(this_plot)%xmax= plot_size_sig(this_plot)%xmax *sig_plot(2*this_plot-1)
          ! plot_size(this_plot)%ymin= plot_size_sig(this_plot)%ymin *sig_plot(2*this_plot)
          ! plot_size(this_plot)%ymax= plot_size_sig(this_plot)%ymax *sig_plot(2*this_plot)
         call set_coordinate_fields
         call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
  
             else
                IF(gEnqKeyState_et(-1)) THEN
  
         !        write(6,*) actlist%x,actlist%y
                 ray%ND=n_ND
         !        IF(.not.gEnqKeyState_et(ichar("P"))) ray%r=0.0_dp
                 ray%NTURNS=n_turns
                 ray%plane=this_plot
                 if(gEnqKeyState_et(ichar("F"))) then
                  actlist%x=0.d0;actlist%y=0.d0;
                 endif
                 IF(gEnqKeyState_et(ichar("I"))) then
                 Write(6,*) " Input delta and t"
                  read(5,*) actlist%x,actlist%y
                 endif
                 IF(gEnqKeyState_et(ichar("P"))) then
                    ray%r=previous_ray
                    KEEP_COLOUR=.false.
                 elseif(gEnqKeyState_et(ichar("C"))) then
                  write(6,*) "Keep data in plane",this_plot
                    KEEP_COLOUR=.TRUE.
                 else
                     ray=actlist
                    previous_ray=ray%r
                 endif
                 ray%widget(this_plot)=plot(this_plot)
         !        call egm_scale_draw(plot(1),actlist%x,actlist%y,ray%colour)
                 RAY%IN(THIS_PLOT)=.TRUE.
                 call track_standard(ray,flag_layout,track_a,track_map,turns)
                  ray%stable=.true.
                  ray%aperture=.true.
                call gFlushGraphics
                ENDIF
             endif
  
      else if (callid == (x5_x6_Graphics3+2*GIDENT)) then
         !call e_g_resize_graphics(plot(this_plot),actlist,done)
         ! if(done) then
         !      call Enq_coordinate_fields
         !      call set_coordinate_fields
         	
         !      call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
         !      call gmDrawWindow(Form_frame_size )
         !endif
         !done=.false.
  
      else if (callid == (PLOT_ring)) then
         IF(gEnqKeyState_et(ichar("S"))) then
         write(6,*) " Sizes "
         call gmEnqGraphicsPixelBounds(PLOT_ring_Graphics4 ,gpix )
         ! write(6,*) gpix%xmin,gpix%xmax,gpix%xmax-gpix%xmin+1
         ! write(6,*) gpix%ymin,gpix%ymax,gpix%ymax-gpix%ymin+1
         write(6,*) " Pixel Width  ",gpix%xmax-gpix%xmin+1
         write(6,*) " Pixel Height ",gpix%ymax-gpix%ymin+1
  
         call gmEnqGraphicsFrameBounds(PLOT_ring_Graphics4 ,glim )
         ! write(6,*) glim%xmin,glim%xmax,glim%xmax-glim%xmin+1
         ! write(6,*) glim%ymin,glim%ymax,glim%ymax-glim%ymin+1
         write(6,*) " Width  ",glim%xmax-glim%xmin
         write(6,*) " Height ",glim%ymax-glim%ymin
         endif
  
      else if (callid == (PLOT_ring+GIDENT)) then
         this_plot=4
  
         IF(gEnqKeyState_et(ichar("S"))) then
         write(6,*) " Sizes "
         call gmEnqGraphicsPixelBounds(PLOT_ring_Graphics4 ,gpix )
         ! write(6,*) gpix%xmin,gpix%xmax,gpix%xmax-gpix%xmin+1
         ! write(6,*) gpix%ymin,gpix%ymax,gpix%ymax-gpix%ymin+1
         write(6,*) " Pixel Width  ",gpix%xmax-gpix%xmin+1
         write(6,*) " Pixel Height ",gpix%ymax-gpix%ymin+1
  
         call gmEnqGraphicsFrameBounds(PLOT_ring_Graphics4 ,glim )
         ! write(6,*) glim%xmin,glim%xmax,glim%xmax-glim%xmin+1
         ! write(6,*) glim%ymin,glim%ymax,glim%ymax-glim%ymin+1
         write(6,*) " Width  ",glim%xmax-glim%xmin
         write(6,*) " Height ",glim%ymax-glim%ymin
         endif
         ! IF(gEnqKeyState_et(ichar("S"))) then
         ! write(6,*) " Resizing "
         !call gmEnqGraphicsPixelBounds(PLOT_ring_Graphics4 ,gpix )
         ! write(6,*) " Pixel Width  ",gpix%xmax-gpix%xmin+1
         !write(6,*) " Pixel Height ",gpix%ymax-gpix%ymin+1
  
         !call gmEnqGraphicsFrameBounds(PLOT_ring_Graphics4 ,glim )
         !write(6,*) " Width  ",glim%xmax-glim%xmin+1
         ! write(6,*) " Height ",glim%ymax-glim%ymin+1
         !endif
  
      else if (callid == (m_autos)) then
               this_plot=4
               call e_gm_rescale2d(plot(this_plot),my_ering)
               call gmEnqGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
  
      else if (callid == (mu_auto)) then
               this_plot=4
               call e_gm_rescale2d_universe(plot(this_plot),m_u)
               call gmEnqGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
      else if (callid == (PLOT_ring_Clear_entry)) then
               this_plot=4
               call gmClearGraphicsFrame(plot(this_plot))
  
      else if (callid == (PLOT_ring_draw_universe_entry)) then
               this_plot=4
         call gmActivateGraphicsFrame(plot(this_plot))
         call gmClearGraphicsFrame(plot(this_plot))
         l_dummy=>m_U%start
               do i1=1,m_u%n
                call e_gm_draw_magnets(l_dummy,colour_magnet,plot(this_plot))
         l_dummy=>l_dummy%next
               enddo
  
      else if (callid == (PLOT_ring_Draw_Trackable_Universe_entry)) then
               this_plot=4
  
         if(m_t%n>0) then
         l_dummy=>m_t%start
               do i1=1,m_t%n
                call e_gm_draw_magnets(l_dummy,colour_magnet,plot(this_plot))
         l_dummy=>l_dummy%next
               enddo
         endif
      else if (callid == (PLOT_ring_Draw_DNA_entry)) then
               this_plot=4
         !      call gmClearGraphicsFrame(plot(this_plot))
  
  
               if(associated(my_ering%dna)) then
                 write(6,*) "Number ",size(my_ering%dna)," in DNA"
                 write(6,*) "Data base layouts "
                 write(6,*) my_ering%dna(:)%counter
               do i1=1,size(my_ering%dna)
                 call e_gm_draw_magnets(my_ering%dna(i1)%l,colour_magnet,plot(this_plot))
               enddo
               else
                call locate_in_universe(my_ering%start,i1,i2)
                write(6,*) " Data base layout",i1," is its own DNA"
               endif
      else if (callid == (m_drawl)) then
               this_plot=4
         !      call gmClearGraphicsFrame(plot(this_plot))
               call e_gm_draw_layout(my_ering,colour_layout,plot(this_plot))
      else if (callid == (m_drawmag)) then
         this_plot=4
         call e_gm_draw_magnets(my_ering,colour_magnet,plot(this_plot))
      else if (callid == (PLOT_ring_Erase_and_Draw_Magnets_entry)) then
         this_plot=4
             call gmActivateGraphicsFrame(plot(this_plot))
         call gmClearGraphicsFrame(plot(this_plot))
         call e_gm_draw_magnets(my_ering,colour_magnet,plot(this_plot))
  
      else if (callid == (PLOT_ring_Graphics4_Rescale_and_Draw_entry)) then
         this_plot=4
               call e_gm_rescale2d(plot(this_plot),my_ering)
               call gmEnqGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
         call e_gm_draw_magnets(my_ering,colour_magnet,plot(this_plot))
  
      else if (callid == (m_plot_ray)) then
         if(flag_layout/=0) then
          flag_layout=0
                call gmSetWidgetProp(m_plot_ray,gmLabel="Plot Ray")
         else
          flag_layout=flag_layout_default
          call gmSetWidgetProp(m_plot_ray,gmLabel="Do not Plot Ray")
         endif
      else if (callid == (m_scale_plot)) then
         this_scale=0
         call print_scales(Form_scale_val)
         call gmDrawWindow(Form_scale)
  
  
      else if (callid == (m_p_fix0)) then
         call e_gm_draw_fix(my_ering,.true.,colour_plot,plot(4),scale_info(0))
      else if (callid == (m_p_fix)) then
         call e_gm_draw_fix(my_ering,.false.,colour_plot,plot(4),scale_info(0))
      else if (callid == (PLOT_ring_Compute_Lattice_Functions_entry)) then
         turns=1
         call compute_lattice_function
         call gmSetWidgetStatus(M_select, GSELECTABLE)
  
  
      else if (callid == (M_select)) then
               call gmSetValueSetting(Form_lat_i ,i_lat )
               call gmSetValueSetting(Form_lat_j ,j_lat )
               call gmSetValueSetting(Form_lat_k ,k_lat )
               n_lat=lat(i_lat,j_lat,k_lat)
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
           call gmSetValueSetting(Form_lat_s ,fib%i%s)
  
           call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
  
         !!!!!!!!!!!!!!
                 k_lat=gmEnqValueSetting(Form_lat_k )
                 n_lat=lat(i_lat,j_lat,k_lat)
         call gmEnqWidgetInfo(M_plotbetax,gwid)
         gwid%label="Plot "//lat_name(n_lat)
         call gmSetWidgetInfo(M_plotbetax,gwid)
         call gmEnqWidgetInfo(m_scalebetax,gwid)
         gwid%label="Scale for "//lat_name(n_lat)
         call gmSetWidgetInfo(m_scalebetax,gwid)
  
  
  
               call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmSetWidgetStatus(M_auto_scale, GSELECTABLE)
  
         !!!!!!!!!!!!!!
         call gmDrawWindow(Form_lat )
      else if (callid == (M_plotbetax)) then
         this_scale=n_lat
         call e_gm_draw_info(colour_plot,plot(4))
      else if (callid == (m_scalebetax)) then
         this_scale=n_lat
         call print_scales(Form_scale_val)
         call gmDrawWindow(Form_scale)
      else if (callid == (M_auto_scale)) then
         call auto_scale_info(my_ering,PLOT_ring_Graphics4,0.07,n_lat)
         this_scale=n_lat
         call print_scales(Form_scale_val)
  
      else if (callid == (PLOT_ring_Plot_Dipole_Component_entry)) then
         this_plot=4
         call e_gm_draw_muls(my_ering,colour_magnet,1,plot(this_plot))
  
      else if (callid == (PLOT_ring_Plot_Quadrupole_Componeny_entry)) then
         this_plot=4
         call e_gm_draw_muls(my_ering,colour_magnet,2,plot(this_plot))
  
      else if (callid == (PLOT_ring_Plot_Sextupole_Component_entry)) then
         this_plot=4
         call e_gm_draw_muls(my_ering,colour_magnet,3,plot(this_plot))
  
      else if (callid == (PLOT_ring_Plot_Soleninoid_Component_entry)) then
         this_plot=4
         call e_gm_draw_muls(my_ering,colour_magnet,0,plot(this_plot))
      else if (callid == (PLOT_ring_Colour_of_Dipole_Component_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_mul(1),red, green, blue)
  
      else if (callid == (PLOT_ring_Colour_of_Quadrupole_Component_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_mul(2),red, green, blue)
  
      else if (callid == (PLOT_ring_Colour_of_Sextupole_Component_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_mul(3),red, green, blue)
  
      else if (callid == (PLOT_ring_Colour_of_Solenoid_Component_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_mul(0),red, green, blue)
  
      else if (callid == (PLOT_ring_Scale_entry)) then
         !write(6,*) " dxmag = ",dxmag
         !read(5,*) dxmag
                  call gmDrawWindow( Form_magnet_scale)
      else if (callid == (PLOT_ring_Scale_of_Misalignments_entry)) then
         write(6,*) " Input Magnification factor ",y_mis_plot
         read(5,*) y_mis_plot
      else if (callid == (M_colour)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_magnet,red, green, blue)
  
      else if (callid == (M_col_bends)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_bend,red, green, blue)
      else if (callid == (PLOT_ring_Colour_of_Solenoids_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_sol,red, green, blue)
  
      else if (callid == (PLOT_ring_Colour_of_Cavities_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_cavity,red, green, blue)
  
      else if (callid == (PLOT_ring_Copy_to_Clipboard_entry)) then
                  call gmCopyGraphicsToClipboard(PLOT_ring_Graphics4 ,GCOPY )
  
      else if (callid == (PLOT_ring_Help_on_holding)) then
               call gmDrawWindow(Form_explain_layout )
      else if (callid == (PLOT_ring_Exit_Graphical_Interface_entry)) then
                  close(mstable)
                   mstable=0
         exit
  
      else if (callid == (PLOT_ring_Graphics4)) then
         IF(gEnqKeyState_et(-20))  then
           ! write(6,*) " 2nd boundary "
          !         limit_temp%xmax=actlist%x
          !         limit_temp%ymax=actlist%y
            d(1)=limit_temp%xmin-actlist%x
            d(2)=limit_temp%ymin-actlist%y
  
            call  gmEnqGraphicsFrameBounds(plot(this_plot),limit_temp)
            limit_temp%xmin=limit_temp%xmin+d(1)
            limit_temp%ymin=limit_temp%ymin+d(2)
            limit_temp%xmax=limit_temp%xmax+d(1)
            limit_temp%ymax=limit_temp%ymax+d(2)
            call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
         endif
      else if (callid == (PLOT_ring_Graphics4+GIDENT)) then
         this_plot=4
  
          ngirder=-1
          nsiamese=-1
  
         !IF(gEnqKeyState_et(-1)) then
         !call e_gm_locate_special(plot(this_plot),actlist%x,actlist%y,name,i)
         !GOTO 5000
         !endif
         if(gEnqKeyState_et(ichar("Z"))) then
         write(6,*) "Z"
         call e_gm_locate_pointer(plot(this_plot),actlist%x,actlist%y,name,i)
          endif
         if((gEnqKeyState_et(ichar("G")).or.gEnqKeyState_et(ichar("H"))).and.gEnqKeyState_et(-1)) then
         !write(6,*) "G"
         call e_gm_locate_pointer_gird(plot(this_plot),actlist%x,actlist%y,name,i,.false.)
           call move_to(my_ering,fib,i)
         !call e_gm_draw_magnet_girder(plot(this_plot),fib,girder_colour,ngirder)
          if(associated(fib%mag%siamese)) then
           call gmDefineTimerCallback(time_siamese,siamese_flash)
           nsiamese=0
           call write_siamese(fib)
          endif
         !girder_flash=100003,time_girder=300,ngirder=0
         if( gEnqKeyState_et(ichar("H")) ) then
            write(6,*) "  additive ?"
            read(5,*)  adderror
           write(6,*) " dx,dy,dz,thetax,thetay,thetaz"
            read(5,*) fix3
           call MISALIGN_siamese(fib,fix3,add=adderror)
         endif
          endif
         !  gEnqKeyState_et(-3) -> Right mouse
         if((gEnqKeyState_et(ichar("G")).or.gEnqKeyState_et(ichar("H"))).and.gEnqKeyState_et(-3)) then
         !write(6,*) "G"
         call e_gm_locate_pointer_gird(plot(this_plot),actlist%x,actlist%y,name,i,.false.)
           call move_to(my_ering,fib,i)
         !call e_gm_draw_magnet_girder(plot(this_plot),fib,girder_colour,ngirder)
          if(associated(fib%mag%girderS)) then
           call gmDefineTimerCallback(time_girder,girder_flash)
           ngirder=0
           if(associated(fib%mag%assembly)) then
            call write_girders(fib)
  
          !  write(6,*) "Assembly # ",fib%mag%assembly%i
          !  write(6,*) " # in original assembly ",fib%mag%assembly%mul
          !  write(6,*) " # added to assembly ",fib%mag%assembly%added
           endif
          endif
         !girder_flash=100003,time_girder=300,ngirder=0
         if( gEnqKeyState_et(ichar("H")) ) then
            write(6,*) "  additive ?"
            read(5,*)  adderror
           write(6,*) " dx,dy,dz,thetax,thetay,thetaz"
            read(5,*) fix3
           call MISALIGN_GIRDER(fib,fix3,add=adderror)
         endif
  
          endif
  
  
         IF(gEnqKeyState_et(ichar("L")).or.gEnqKeyState_et(ichar("S")) &
         .or.gEnqKeyState_et(ichar("C")).or.gEnqKeyState_et(ichar("A")) &
         .or.gEnqKeyState_et(ichar("P")).or.gEnqKeyState_et(ichar("M")) &
         .or.gEnqKeyState_et(ichar("B")).or.gEnqKeyState_et(ichar("O"))) THEN
          write(6,*) " Actual Location "
          write(6,*) actlist%x,actlist%y
         call e_gm_locate_pointer_gird(plot(this_plot),actlist%x,actlist%y,name,i,.true.)
           dis_pos=i
           call move_to(my_ering,fib,i)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetValueSetting(Form_lat_s,fib%i%s)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
  
         IF(gEnqKeyState_et(ichar("L"))) then
            call e_locate_bpm(fib)
            if(allocated(monitors)) then
             if(ibpm/=0) then
               pbpm=>monitors(ibpm)%p
               call gmSetTextSetting(wnbpm ,pbpm%mag%name)
               call gmSetValueSetting(wibpm ,ibpm)
               call e_gm_draw_magnet(PLOT_ring_Graphics4,pbpm,colour=gOrange)
               call gFlushGraphics
               call gmSetValueSetting(Form_plot_linear_Value98 ,monitors(ibpm)%kind )
              endif
  
            endif
  
         endif
          if(gEnqKeyState_et(ichar("S"))) then
           my_start=i
               call gmSetTextSetting(Form_States_name,fib%mag%name)
               call gmSetValueSetting(Form_States_start ,my_start )
          around_fix=.false.
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
         call warn_t(fib,Form_States_line)
         !call e_gm_draw_magnet_3d(fib)
           if(around_fix)         then ;  call gmSetToggleSwitch(state(9),gon)
           else;                          call gmSetToggleSwitch(state(9),goff); endif
                    call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                    ray%do_a_for_dyn=.false.
  
          endif
  
  
          if(gEnqKeyState_et(ichar("C"))) then
           write(6,*) " N , BN (Negative N means skew component)"
           read(5,*) npole, xbn
            CALL ADD(fib,npole,0,xbn)
          endif
          if(gEnqKeyState_et(ichar("A"))) then
          WRITE(6,*) " BEGINNING OF FIBRE "
          WRITE(6,*) FIB%CHART%F%A
          WRITE(6,*) " VEC X "
          WRITE(6,*) FIB%CHART%F%ENT(1,:)
          WRITE(6,*) " VEC Y "
          WRITE(6,*) FIB%CHART%F%ENT(2,:)
          WRITE(6,*) " VEC Z "
          WRITE(6,*) FIB%CHART%F%ENT(3,:)
          call e_gm_draw_frame(plot(4),FIB%CHART%F%A,FIB%CHART%F%ENT)
         endif
  
          if(gEnqKeyState_et(ichar("P"))) then
           WRITE(6,*) " GEOMETRY,ENERGY,TIME "
           WRITE(6,*) fib%PATCH%PATCH,  fib%PATCH%ENERGY, fib%PATCH%TIME
            write(6,*)" directions ",fib%dir,fib%previous%dir,fib%next%dir
           if(fib%PATCH%PATCH==1.or.fib%PATCH%PATCH==3) then
            write(6,*)" pi rotations ", fib%patch%A_X1,fib%patch%A_X2
            write(6,*) " d ", fib%patch%A_d
            write(6,*) " ang ",fib%patch%A_ang
           elseif(fib%PATCH%PATCH==2.or.fib%PATCH%PATCH==3) then
            write(6,*)" pi rotations ", fib%patch%B_X1,fib%patch%B_X2
            write(6,*) " d ", fib%patch%b_d
            write(6,*) " ang ",fib%patch%b_ang
           endif
          endif
          if(gEnqKeyState_et(ichar("B"))) then
           WRITE(6,*) " END OF FIBRE "
           WRITE(6,*) FIB%CHART%F%B
           WRITE(6,*) " VEC X "
           WRITE(6,*) FIB%CHART%F%EXI(1,:)
           WRITE(6,*) " VEC Y "
           WRITE(6,*) FIB%CHART%F%EXI(2,:)
           WRITE(6,*) " VEC Z "
           WRITE(6,*) FIB%CHART%F%EXI(3,:)
         call e_gm_draw_frame(plot(4),FIB%CHART%F%B,FIB%CHART%F%EXI)
  
          endif
          if(gEnqKeyState_et(ichar("M"))) then
           WRITE(6,*) " MIDDLE OF FIBRE "
           WRITE(6,*) FIB%CHART%F%o
           WRITE(6,*) " VEC X "
           WRITE(6,*) FIB%CHART%F%mid(1,:)
           WRITE(6,*) " VEC Y "
           WRITE(6,*) FIB%CHART%F%mid(2,:)
           WRITE(6,*) " VEC Z "
           WRITE(6,*) FIB%CHART%F%mid(3,:)
         call e_gm_draw_frame(plot(4),FIB%CHART%F%O,FIB%CHART%F%mid)
  
          endif
          if(gEnqKeyState_et(ichar("O"))) then
           WRITE(6,*) " CENTRE OF PLOTING USING (A,ENT)"
           WRITE(6,*)fib%mag%name
           WRITE(6,*) FIB%CHART%F%A
           WRITE(6,*) " VEC X "
           WRITE(6,*) FIB%CHART%F%ENT(1,:)
           WRITE(6,*) " VEC Y "
           WRITE(6,*) FIB%CHART%F%ENT(2,:)
           WRITE(6,*) " VEC Z "
           WRITE(6,*) FIB%CHART%F%ENT(3,:)
          ! A_GINO=FIB%CHART%F%A
           ent_gino=FIB%CHART%F%MID
          endif
  
  
         endif
  
         if(gEnqKeyState_et(ichar("0"))) then
           write(6,*) " Location"
                   cg(1)=actlist%x
                   cg(2)=actlist%y
  
                   write(6,*) " x , z",cg(2),cg(1)
  
         endif
  
  
  
         if(gEnqKeyState_et(ichar("1"))) then
           write(6,*) " 1st boundary "
                   limit_temp%xmin=actlist%x
                   limit_temp%ymin=actlist%y
         endif
  
         if(gEnqKeyState_et(ichar("2")).or.gEnqKeyState_et(ichar("4"))) then
           write(6,*) " 2nd boundary "
                   limit_temp%xmax=actlist%x
                   limit_temp%ymax=actlist%y
                   write(6,*) sqrt((actlist%x-limit_temp%xmin)**2+(actlist%y-limit_temp%ymin)**2)
  
               !    write(6,*) limit_temp
                   IF(gEnqKeyState_et(ichar("2"))) then
                      call e_g_order(limit_temp,limit_temp)
               !    write(6,*) limit_temp
                      call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
                   ENDIF
         endif
  
         if(gEnqKeyState_et(ichar("3"))) then
           ! write(6,*) " 2nd boundary "
          !         limit_temp%xmax=actlist%x
          !         limit_temp%ymax=actlist%y
            d(1)=limit_temp%xmin-actlist%x
            d(2)=limit_temp%ymin-actlist%y
  
            call  gmEnqGraphicsFrameBounds(plot(this_plot),limit_temp)
            limit_temp%xmin=limit_temp%xmin+d(1)
            limit_temp%ymin=limit_temp%ymin+d(2)
            limit_temp%xmax=limit_temp%xmax+d(1)
            limit_temp%ymax=limit_temp%ymax+d(2)
            call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
         endif
  
         IF(gEnqKeyState_et(-20))  then
                   limit_temp%xmin=actlist%x
                   limit_temp%ymin=actlist%y
         endif
  
  
         5000 CONTINUE
      else if (callid == (PLOT_ring_Graphics4+2*GIDENT)) then
         !call e_g_resize_graphics(plot(this_plot),actlist,done)
         !if(done) then
         !      call Enq_coordinate_fields
         !      call set_coordinate_fields
         	
         !      call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
         !      call gmDrawWindow(Form_frame_size )
         !endif
         !  done=.false.
  
      else if (callid == (Form_States_Exit_Graphical_Interface_entry)) then
                  close(mstable)
                   mstable=0
         exit
  
      else if (callid == (Form_States_stoch)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_Totalpath)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_time)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_radiation)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_nocavity)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_fringe)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_exactmis)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_spin)) then
         call read_ptc_states(my_estate)
         call print(my_estate,6)
      else if (callid == (Form_States_lingyun)) then
         if(gmEnqToggleSwitch_et(Form_States_lingyun ))then
          call change_package(1)
         else
          call change_package(2)
  
         endif
  
          if(lingyun_yang) then
           write(6,*) "Chinese Package"
          call gmSetToggleSwitch(Form_States_lingyun ,gon )
          else
           write(6,*) "Germanic Package"
          call gmSetToggleSwitch(Form_States_lingyun ,goff )
          endif
      else if (callid == (Form_States_only4d)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_delta)) then
         call read_ptc_states(my_estate)
      else if (callid == (Form_States_my_order)) then
               my_order= gmEnqValueSetting(Form_States_my_order )
      else if (callid == (Form_States_Button63)) then
          if(.not.associated(MY_eRING%t))then
           write(6,*) "Making a node layout "
           call MAKE_NODE_LAYOUT(MY_eRING)
          endif
            call FILL_SURVEY_DATA_IN_NODE_LAYOUT(MY_eRING)
            call move_to(my_ering,fib,my_start)
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
         call warn_t(fib,Form_States_line)
      else if (callid == (Form_States_Button79)) then
         call TRACK_fill_ref(my_eRING,my_FIX,my_start,my_eSTATE)
  
      else if (callid == (Form_States_start_t)) then
         !i=my_start_t
  
         !       my_start_t   = gmEnqValueSetting(th(4) )
  
         !if(my_start_t/=0) then
         ! if(.not.((gmEnqValueSetting(th(2))<=my_start_t) .and. &
         !(gmEnqValueSetting(th(3))>=my_start_t))) then
         !         my_start_t   = i
         !         call gmSetValueSetting(th(4) ,i )
  
         ! endif
         !endif
      else if (callid == (Form_States_Gauge_t)) then
             i=my_start_t
             my_start_t = nint(gmEnqGaugeValue(th(5) )+0.1)
            call move_to(my_ering,fib,my_start)
         if(associated(fib%parent_layout%t))then
           call gmSetValueSetting(th(2),fib%t1%pos )
           call gmSetValueSetting(th(3),fib%t2%pos )
           if(my_start_t>fib%t2%pos) my_start_t=fib%t2%pos
             call gmSetGaugeValue(th(5) ,my_start_t )
           call gmSetValueSetting(th(4),my_start_t )
          if(my_start_t/=fib%t1%pos.and. i/=my_start_t) then
                   call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                   ray%do_a_for_dyn=.false.
          elseif(my_start_t==fib%t1%pos.and. i/=my_start_t)then
            my_start_t=0
                  call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                   ray%do_a_for_dyn=.false.
                  call gmSetGaugeValue(th(5) ,fib%t1%pos )
                  call gmSetValueSetting(th(4),fib%t1%pos )
           elseif(my_start_t/=fib%t1%pos.and. i==my_start_t)then
           elseif(my_start_t==fib%t1%pos.and. i==my_start_t)then
            my_start_t=0
                  call gmSetGaugeValue(th(5) ,fib%t1%pos )
                  call gmSetValueSetting(th(4),fib%t1%pos )
         endif
  
         !  if(my_start_t==fib%t1%pos+fib%mag%p%nst/2+2) then
         !   call gmSetWidgetProp(th(5) ,gmIconfile='MSML1061')
         !  else
         !   call gmSetWidgetProp(th(5) ,gmIconfile='MSML1012')
         !  endif
         else
           call gmSetValueSetting(th(2),0 )
           call gmSetValueSetting(th(3),0 )
           call gmSetValueSetting(th(4),0 )
           my_start_t=0
         !   call gmSetWidgetProp(th(5) ,gmIcon='MBIG1100')
         endif
         !  call gmDrawWidget(th(5))
  
         !th(6)=gmGetIconResource('MSML1061')
         !th(7)=gmGetIconResource('MSML1012')
         !th(8)=gmGetIconResource('MBIG1100')
          if(.not.associated(MY_eRING%t))then
                     call gmSetTextSetting(Form_warn_gauge_Text52 , "Make a Node Layout First" )
                     call gmDrawWindow(Form_warn_gauge)
          endif
         call warn_t(fib,Form_States_line)
  
      else if (callid == (Form_States_Button64)) then
          call gmDrawWindow(Form_help_dyn)
  
      else if (callid == (Form_States_fixpoint)) then
         if(gmEnqToggleSwitch_et(state(9))) then
          around_fix=.true.
          MY_FIX=0.D0
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,eps_gino,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions(fix=my_fix)
         else
          around_fix=.false.
         endif
      else if (callid == (Form_States_nturn)) then
               n_turns= gmEnqValueSetting(Form_States_nturn )
         deallocate(zturns)
         allocate(zturns(6,0:n_turns,zturns_nrays))
         zturns=zero
         call gmSetWidgetStatus(but1, GunSELECTABLE)
         call gmSetWidgetStatus(but2, GunSELECTABLE)
  
      else if (callid == (Form_States_start)) then
            my_start= gmEnqValueSetting(Form_States_start )
            if(my_start<=0) then
             my_start=my_ering%n+my_start
            elseif(my_start>my_ering%n) then
             my_start=mod(my_start,my_ering%n)
            endif
  
            dis_pos=my_start
            call move_to(my_ering,fib,my_start)
           call gmSetTextSetting(Form_states_name ,fib%mag%name)
           write(6,*) my_start,fib%mag%name
           write(6,*) "geometry patch ",fib%patch%patch
           do i=1,fib%mag%p%nmul
           write(6,*) i,fib%mag%an(i),fib%mag%bn(i)
           enddo
         call fill_t(fib,th)
         my_start_t= gmEnqValueSetting( th(4) )
  
         if(around_fix) then
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
  
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetValueSetting(Form_lat_s,fib%i%s)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
                    call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                    ray%do_a_for_dyn=.false.
         call warn_t(fib,Form_States_line)
         call gmActivateGraphicsFrame(plot(4))
         call e_gm_draw_magnet(plot(4),fib,colour=gred)
         call gFlushGraphics
         call write_siamese(fib)
  
          call gmSetValueSetting(Form_States_start,my_Start )
  
  
      else if (callid == (Form_States_Patch)) then
           WRITE(6,*) " GEOMETRY,ENERGY,TIME "
           WRITE(6,*) fib%PATCH%PATCH,  fib%PATCH%ENERGY, fib%PATCH%TIME
            write(6,*)" directions ",fib%previous%dir,fib%dir,fib%next%dir
                   if(fib%PATCH%PATCH==1.or.fib%PATCH%PATCH==3) then
  
                     write(6,*)" pi rotations ", fib%patch%A_X1,fib%patch%A_X2
                     write(6,*) " d ", fib%patch%A_d
                     write(6,*) " ang ",fib%patch%A_ang
                    elseif(fib%PATCH%PATCH==2.or.fib%PATCH%PATCH==3) then
  
                     write(6,*)" pi rotations ", fib%patch%B_X1,fib%patch%B_X2
                     write(6,*) " d ", fib%patch%b_d
                     write(6,*) " ang ",fib%patch%b_ang
                    endif
  
      else if (callid == (Form_States_first_fib)) then
            first_fib= gmEnqValueSetting(Form_States_first_fib )
  
      else if (callid == (Form_States_second_fib)) then
           second_fib= gmEnqValueSetting(Form_States_second_fib )
  
  
      else if (callid == (Form_States_my_delta)) then
               my_delta= gmEnqValueSetting(Form_States_my_delta )
          my_fix(5)=my_delta
         if(around_fix) then
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
                    call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                    ray%do_a_for_dyn=.false.
  
         endif
  
      else if (callid == (Form_States_name)) then
         call gmEnqTextSetting(Form_States_name ,name )
         call context(name)
         call move_to_partial( my_ering,fib,name,i)
         if(i/=0)my_start=i
  
           call move_to(my_ering,fib,my_start)
           call gmSetTextSetting(Form_states_name ,fib%mag%name)
           call gmSetValueSetting(Form_States_start,my_start )
  
         call fill_t(fib,th)
  
         my_start_t= gmEnqValueSetting( th(4) )
  
  
         if(around_fix.and.(i/=0)) then
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
         call warn_t(fib,Form_States_line)
  
         dis_pos=my_start
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetValueSetting(Form_lat_s,fib%i%s)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
                    call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                    ray%do_a_for_dyn=.false.
         call gmActivateGraphicsFrame(plot(4))
         call e_gm_draw_magnet(plot(4),fib,colour=gred)
         call gFlushGraphics
         call write_girders(fib)
  
  
  
  
      else if (callid == (Form_States_fix0)) then
         if(gmEnqToggleSwitch_et(state(10))) then
          store_fix0=.true.
         else
          store_fix0=.false.
         endif
      else if (callid == (Form_States_store)) then
         call store_fix(my_ering)
      else if (callid == (Form_States_aperture_check)) then
         if(gmEnqToggleSwitch_et(state(12))) then
          c_%APERTURE_FLAG=.true.
         else
          c_%APERTURE_FLAG=.false.
         endif
      else if (callid == (Form_States_apx)) then
               x_ap= gmEnqValueSetting(Form_States_apx )
  
      else if (callid == (Form_States_apy)) then
               y_ap= gmEnqValueSetting(Form_States_apy )
  
      else if (callid == (Form_States_name_ap)) then
         call gmEnqTextSetting(Form_States_name_ap ,name_ap )
         call context(name_ap)
         call scan_aperture(my_ering,Form_States_apx,Form_States_apy)
      else if (callid == (Form_States_filename_ap)) then
         call gmEnqTextSetting(Form_States_filename_ap ,filename_ap )
  
      else if (callid == (Form_States_ascript)) then
         call read_ptc_states(my_estate)
         if(gmEnqToggleSwitch_et(state(11))) then
         if(filename_ap/=' ') then
           call move_to(my_ering,fib,my_start)
           name=fib%mag%name
           file_ap=newfile
           open(unit=file_ap,file=filename_ap)
           write(file_ap,'(i4,1x,a16,1x,f8.4,1x,f8.4)') my_start,name,x_ap,y_ap
         endif
         else
         if(file_ap>0) file_ap=closefile
         endif
      else if (callid == (Form_States_comp_ascript)) then
         turns=1
         call compute_map("map.txt","a.txt","a_inv.txt")
      else if (callid == (Form_States_track_map)) then
         if(gmEnqToggleSwitch_et(state(13))) then
          track_map=.true.
         else
          track_map=.false.
         endif
      else if (callid == (Form_States_Button77)) then
              call store_aperture(my_ering)
         write(6,*) kind_ap
      else if (callid == (Form_States_fibre1)) then
         if(gmEnqToggleSwitch_et(Form_States_fibre1 ))then
          select_gino_12=1
         else
          select_gino_12=2
         endif
  
         write(6,*) "select_gino_12", select_gino_12
  
      else if (callid == (Form_States_Button82+GIDENT)) then
         nullify(p_gino_1)
         nullify(p_gino_2)
  
      else if (callid == (Form_States_remove_ap)) then
              call remove_all_aperture(my_ering)
         write(6,*) kind_ap
      else if (callid == (Form_States_gird_siam_check+GIDENT)) then
         call check_siamese_girder_topology(m_U,-1000)
  
      else if (callid == (Form_scale_val)) then
         !this_scale=n_lat
         call read_scales(Form_scale_val)
      else if (callid == (m_quad_mis)) then
         call read_sig_mis
         call misalign(my_ering,1)
         if(around_fix) then
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
  
      else if (callid == (m_move_dip)) then
         call read_sig_mis
         call misalign(my_ering,2)
         if(around_fix) then
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
  
      else if (callid == (M_move_sex)) then
         call read_sig_mis
         call misalign(my_ering,3)
         if(around_fix) then
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
  
      else if (callid == (m_all_mis)) then
         call read_sig_mis
         do i=1,nfam_mis
          call misalign(my_ering,i)
         enddo
         if(around_fix) then
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
  
  
      else if (callid == (Form_mis_11)) then
         call read_sig_mis
      else if (callid == (Form_mis_21)) then
         call read_sig_mis
      else if (callid == (Form_mis_31)) then
         call read_sig_mis
      else if (callid == (Form_mis_10)) then
         call read_sig_mis
      else if (callid == (Form_mis_20)) then
         call read_sig_mis
      else if (callid == (Form_mis_30)) then
         call read_sig_mis
      else if (callid == (lat_ctc)) then
         call compute_nonlinear_tune(q,dtune)
               call gmSetValueSetting(Form_lat_qx ,q(1,0) )
               call gmSetValueSetting(Form_lat_qy ,q(2,0) )
               call gmSetValueSetting(Form_lat_qxp ,q(1,1) )
               call gmSetValueSetting(Form_lat_qyp ,q(2,1) )
               call gmSetValueSetting(Form_lat_qxpp ,q(1,2) )
               call gmSetValueSetting(Form_lat_qypp ,q(2,2) )
               call gmSetValueSetting(Form_lat_d11 ,dtune(1))
               call gmSetValueSetting(Form_lat_d22 ,dtune(2))
               call gmSetValueSetting(Form_lat_d12 ,dtune(3))
  
  
      else if (callid == (Form_lat_Exit_Graphical_Interface_entry)) then
                  close(mstable)
         exit
  
      else if (callid == (Form_lat_i)) then
                 i_lat=gmEnqValueSetting(Form_lat_i )
                 n_lat=lat(i_lat,j_lat,k_lat)
         call gmEnqWidgetInfo(M_plotbetax,gwid)
         gwid%label="Plot "//lat_name(n_lat)
         call gmSetWidgetInfo(M_plotbetax,gwid)
         call gmEnqWidgetInfo(m_scalebetax,gwid)
         gwid%label="Scale for "//lat_name(n_lat)
         call gmSetWidgetInfo(m_scalebetax,gwid)
         write(lattice_function_file,*)i_lat,"_",j_lat,"_",k_lat,".dat"
         call context(lattice_function_file)
               call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmSetTextSetting( Form_lat_file, lattice_function_file)
  
  
      else if (callid == (Form_lat_j)) then
                 j_lat=gmEnqValueSetting(Form_lat_j )
                 if(j_lat<i_lat) i_lat=j_lat
                 call gmSetValueSetting(Form_lat_i,i_lat )
                 n_lat=lat(i_lat,j_lat,k_lat)
         call gmEnqWidgetInfo(M_plotbetax,gwid)
         gwid%label="Plot "//lat_name(n_lat)
         call gmSetWidgetInfo(M_plotbetax,gwid)
         call gmEnqWidgetInfo(m_scalebetax,gwid)
         gwid%label="Scale for "//lat_name(n_lat)
         call gmSetWidgetInfo(m_scalebetax,gwid)
         write(lattice_function_file,*)i_lat,"_",j_lat,"_",k_lat,".dat"
         call context(lattice_function_file)
  
  
  
               call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmSetTextSetting( Form_lat_file, lattice_function_file)
  
      else if (callid == (Form_lat_k)) then
                 k_lat=gmEnqValueSetting(Form_lat_k )
                 n_lat=lat(i_lat,j_lat,k_lat)
         call gmEnqWidgetInfo(M_plotbetax,gwid)
         gwid%label="Plot "//lat_name(n_lat)
         call gmSetWidgetInfo(M_plotbetax,gwid)
         call gmEnqWidgetInfo(m_scalebetax,gwid)
         gwid%label="Scale for "//lat_name(n_lat)
         call gmSetWidgetInfo(m_scalebetax,gwid)
         write(lattice_function_file,*)i_lat,"_",j_lat,"_",k_lat,".dat"
         call context(lattice_function_file)
  
  
  
               call gmSetTextSetting(Form_lat_name,lat_name(n_lat))
           call move_to(my_ering,fib,dis_pos)
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmSetTextSetting( Form_lat_file, lattice_function_file)
  
      else if (callid == (Form_mres1)) then
                 mres(1)=gmEnqValueSetting(Form_mres1 )
  
      else if (callid == (Form_mres2)) then
                 mres(2)=gmEnqValueSetting(Form_mres2 )
  
      else if (callid == (Form_mres3)) then
                 mres(3)=gmEnqValueSetting(Form_mres3 )
  
      else if (callid == (Form_lat_Button42)) then
         call print_res_lat(file_res)
      else if (callid == (Form_lat_Button43)) then
         call print_res_(file_res)
      else if (callid == (Form_lat_Button84)) then
           call move_to(my_ering,fib,my_start)
           call e_plot_info(my_ering,fib,n_lat)
      else if (callid == (Form_lat_res_file)) then
                  call gmEnqTextSetting(Form_lat_res_file ,file_res)
         call context(file_res)
      else if (callid == (Form_lat_res_file+7*GIDENT)) then
         file_res=actlist%string
                  call gmSetTextSetting(Form_lat_res_file ,file_res)
      else if (callid == (Form_lat_file)) then
                  call gmEnqTextSetting(Form_lat_file ,lattice_function_file)
         call context(lattice_function_file)
      else if (callid == (Form_lat_file+7*GIDENT)) then
         lattice_function_file=actlist%string
                  call gmSetTextSetting(Form_lat_file ,lattice_function_file)
      else if (callid == (Form_lat_resp)) then
                 mresp=gmEnqValueSetting(Form_lat_resp )
  
      else if (callid == (Form_lat_Button99)) then
         call tune_zturns
         call gmSetWidgetStatus(but1, GunSELECTABLE)
         call gmSetWidgetStatus(but2, GSELECTABLE)
      else if (callid == (Form_lat_Button100)) then
         call am_res_zturns
      else if (callid == (Form_lat_function_monitors)) then
           call move_to(my_ering,fib,my_start)
           call e_plot_info_monitors(my_ering,n_lat)
  
      else if (callid == (Form_dyn+GIDENT)) then
         this_plot=5
      else if (callid == (a_plit_g)) then
         this_plot=5
         call e_gm_draw_grid(gwhite,gblue,20,20,plot(this_plot))
      else if (callid == (M_plot_ap)) then
         call move_to(my_ering,fib,my_start)
  
         if(associated(fib%mag%p%aperture)) then
         if(fib%mag%p%aperture%kind==kind_ap) then
          dh=fib%mag%p%aperture%x
          dv=fib%mag%p%aperture%y
         x=0.0;y=0.0;
          if(around_fix) x=-my_fix(1);y=-my_fix(3);
          call e_gm_draw_rect_g(gcyan,plot(5),x,y,dh,dv)  !,GCOARSEDIAGONALMESH)
         endif
         endif
      else if (callid == (Form_dyn_Grid_Size_entry)) then
         this_plot=5
         write(6,*) " Present xmin and xmax "
         write(6,*) plot_size(this_plot)%xmin,plot_size(this_plot)%xmax
         write(6,*) " give new xmin and xmax "
         read(5,*) plot_size(this_plot)%xmin,plot_size(this_plot)%xmax
         call set_plots_size
         call gmClearGraphicsFrame(plot(this_plot))
         call e_gm_draw_grid(gwhite,gblue,20,20,plot(this_plot))
  
  
      else if (callid == (Form_dyn_Grid_Size_Y_entry)) then
         this_plot=5
         write(6,*) " Present ymin and ymax "
         write(6,*) plot_size(this_plot)%ymin,plot_size(this_plot)%ymax
         write(6,*) " give new ymin and ymax "
         read(5,*) plot_size(this_plot)%ymin,plot_size(this_plot)%ymax
         call set_plots_size
         call gmClearGraphicsFrame(plot(this_plot))
         call e_gm_draw_grid(gwhite,gblue,20,20,plot(this_plot))
  
      else if (callid == (Form_dyn_Use_Mouse_Interface_Dimensions_entry)) then
  
         this_plot=5
  
          plot_size(5)%xmax = plot_size(1)%xmax
          plot_size(5)%xmin = plot_size(1)%xmin
          plot_size(5)%ymax = plot_size(2)%xmax
          plot_size(5)%ymin = plot_size(2)%xmin
  
         call set_plots_size
         call gmClearGraphicsFrame(plot(this_plot))
         call e_gm_draw_grid(gwhite,gblue,20,20,plot(this_plot))
      else if (callid == (Form_dyn_Paste_to_Clipboard_entry)) then
         call gmCopyGraphicsToClipboard(Form_dyn_plot ,Gcopy )
      else if (callid == (Form_dyn_Colour_of_Stable_Ray_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_stable,red, green, blue)
  
      else if (callid == (Form_dyn_Colour_of_Unstable_Ray_entry)) then
               call gmColourControl(red, green, blue)
               call gDefineRGB(colour_unstable,red, green, blue)
  
      else if (callid == (Form_dyn_Exit_Graphic_Interface_entry)) then
                  close(mstable)
                   mstable=0
         exit
  
      else if (callid == (Form_dyn_exit)) then
                  close(mstable)
                  mstable=0
         exit
  
      else if (callid == (Form_dyn_stable)) then
                  filestableb=filestable
                  call gmEnqTextSetting(Form_dyn_stable , filestable )
                  call context(filestable)
                  if(filestable==filestableb) then
                   write(6,*) " Choose a different name "
                   write(6,*) " To overwrite choose a different name"
                  else
                  close(mstable)
                  mstable=0
         !         open(unit=mstable,file=filestable)
                   write(6,'(a26,a120)') " New file for stable rays ",filestable
                   filestableb=filestable
                  endif
  
      else if (callid == (Form_dyn_plot+GIDENT)) then
         !       INQUIRE(UNIT= mstable, EXIST= exists, OPENED= opened)
         ! if(.not.OPENED)open(unit=mstable,file=filestable)
         if(mstable==0) then
          call kanalnummer(mstable)
          open(unit=mstable,file=filestable)
         endif
         if(unmstable==0) then
          call kanalnummer(unmstable)
          open(unit=unmstable,file=unfilestable)
         endif
         this_plot=5
               red=abs(sind(1000.d0*red+1))
         !if(red<0.5) red=red+0.5
               green=abs(sind(1000.d0*red+1))
         !if(green<0.5) green=green+0.5
               blue=abs(sind(1000.d0*green+1))
         !if(blue<0.5) blue=blue+0.5
               call gDefineRGB(ray%colour,red, green, blue)
  
             IF(gEnqKeyState_et(ichar("C"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
             else
                IF(gEnqKeyState_et(-1)) THEN
  
         !        write(6,*) actlist%x,actlist%y
                 ray%ND=2   !n_ND
                 ray%NTURNS=n_turns
                 ray%r=0.d0
                 ray%r(1)=actlist%x
                 ray%r(3)=actlist%y
                 ray%r(5)=my_delta
                 IF(gEnqKeyState_et(ichar("P"))) then
                    ray%r=previous_ray
                 else
                    previous_ray=ray%r
                 endif
                 ray%widget(1:3)=plot(1:3)
         !        call e_gm_draw_cross(ggreen,plot(this_plot),0.005,actlist%x,actlist%y)
                 call e_gm_draw_rect(gblue,plot(this_plot),0.005,actlist%x,actlist%y)
  
         !        call egm_scale_draw(plot(1),actlist%x,actlist%y,ray%colour)
                 RAY%IN=.TRUE.
                 call track_standard(ray,flag_layout,track_a,track_map,turns)
                 if(.not.ray%stable) then
         !        call e_gm_draw_cross(gred,plot(this_plot),0.005,actlist%x,actlist%y)
                 call e_gm_draw_rect(colour_unstable,plot(this_plot),0.005,actlist%x,actlist%y)
                  ray%stable=.true.
                  ray%aperture=.true.
                  write(unmstable,*) actlist%x,actlist%y,my_delta,ray%turn_tracked
                 else
                 call e_gm_draw_rect(colour_stable,plot(this_plot),0.005,actlist%x,actlist%y)
                  write(mstable,*) actlist%x,actlist%y,my_delta,ray%turn_tracked
  
                 endif
                call gFlushGraphics
                ENDIF
             endif
  
  
  
      else if (callid == (Form_dyn_unstable)) then
                  unfilestableb=unfilestable
                  call gmEnqTextSetting(Form_dyn_unstable , unfilestable )
                  call context(unfilestable)
                  if(unfilestable==unfilestableb) then
                   write(6,*) " Choose a different name "
                   write(6,*) " To overwrite choose a different name"
                  else
                  close(unmstable)
                  unmstable=0
         !         open(unit=mstable,file=filestable)
                   write(6,'(a26,a120)') " New file for unstable rays ",unfilestable
                   unfilestableb=unfilestable
                  endif
  
      else if (callid == (Form_dyn_Button76)) then
  
         this_plot=5
  
          plot_size(5)%xmax = plot_size(1)%xmax
          plot_size(5)%xmin = plot_size(1)%xmin
          plot_size(5)%ymax = plot_size(2)%xmax
          plot_size(5)%ymin = plot_size(2)%xmin
  
         call set_plots_size
         call Enq_coordinate_fields
               call set_coordinate_fields
         	
               call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
               call gmDrawWindow(Form_frame_size )
         call gmClearGraphicsFrame(plot(this_plot))
         call e_gm_draw_grid(gwhite,gblue,20,20,plot(this_plot))
  
  
      else if (callid == (Form_dyn_use_a)) then
                   ray%do_a_for_dyn = gmEnqToggleSwitch_et(Form_dyn_use_a )
                   if(ray%do_a_for_dyn) then
                    if(ray%tc==0) then
                     call compute_lattice_function(ray%a)
                    else
                     call compute_lattice_a_for_node(ray%tc,ray%a)
                    endif
                   endif
  
  
      else if (callid == (Form_xpx_n+GIDENT)) then
         this_plot=6
      else if (callid == (Form_xpx_n_G+GIDENT)) then
         this_plot=6
             IF(gEnqKeyState_et(ichar("C"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
         !    else
         !       IF(gEnqKeyState_et(-1)) THEN
         !
         !        ray%ND=n_ND
         !        ray%NTURNS=n_turns
         !        ray%plane=1
         !        if(gEnqKeyState_et(ichar("F"))) then
         !         actlist%x=0.d0;actlist%y=0.d0;
         !        endif
         !        ray=actlist
         !        ray%widget(1)=plot(1)!
         !
         !        RAY%IN(1)=.TRUE.
         !        call track_standard(ray,flag_layout,track_a)
         !         ray%stable=.true.
         !         ray%aperture=.true.
         !       call gFlushGraphics
         !       ENDIF
             endif
             IF(gEnqKeyState_et(ichar("Q"))) THEN
                   write(6,*) actlist%x,actlist%y
                   write(6,*) "Surface of circle",3.1416*(actlist%x**2+actlist%y**2)
             endif
      else if (callid == (Form_xpx_n_G+2*GIDENT)) then
         !this_plot=6
         !call e_g_resize_graphics(plot(this_plot),actlist,done)
         !if(done) then
         !      call Enq_coordinate_fields
         !      call set_coordinate_fields
         	
         !      call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
         !      call gmDrawWindow(Form_frame_size )
         !endif
         ! done=.false.
  
      else if (callid == (Form_ypy_n+GIDENT)) then
         this_plot=7
      else if (callid == (Form_ypy_n_G+GIDENT)) then
         this_plot=7
             IF(gEnqKeyState_et(ichar("C"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
         !    else
         !       IF(gEnqKeyState_et(-1)) THEN
         !
         !        ray%ND=n_ND
         !        ray%NTURNS=n_turns
         !        ray%plane=2
         !        if(gEnqKeyState_et(ichar("F"))) then
         !         actlist%x=0.d0;actlist%y=0.d0;
         !        endif
         !        ray=actlist
         !        ray%widget(2)=plot(2)
         !        RAY%IN(2)=.TRUE.
         !        call track_standard(ray,flag_layout,track_a)
         !         ray%stable=.true.
         !         ray%aperture=.true.
         !       call gFlushGraphics
         !       ENDIF
             endif
  
      else if (callid == (Form_ypy_n_G+2*GIDENT)) then
         !this_plot=7
         !call e_g_resize_graphics(plot(this_plot),actlist,done)
         !if(done) then
         !      call Enq_coordinate_fields
         !      call set_coordinate_fields
         	
         !      call gmSetWidgetProp(Form_frame_size,gmTitle=plot_sizes_name(this_plot))
         !      call gmDrawWindow(Form_frame_size )
         !endif
         !done=.false.
      else if (callid == (Form_magnet_scale_scale)) then
         dxmag = gmEnqValueSetting( Form_magnet_scale_scale)
      else if (callid == (Form_magnet_scale_Value117)) then
         dxframe = gmEnqValueSetting( Form_magnet_scale_Value117)
      else if (callid == (Plot_3D+GIDENT)) then
         this_plot=8
  
      else if (callid == (Form_plot_linear+GIDENT)) then
         this_plot=9
  
      else if (callid == (Form_plot_linear_Graphics+GIDENT)) then
         write(6,*) actlist%x, actlist%y
         this_plot=9
      else if (callid == (Form_plot_linear_ibpm)) then
          i1=ibpm
         if(allocated(monitors)) then
          ibpm= gmEnqValueSettingInteger( Form_plot_linear_ibpm )
          if(ibpm<=size(monitors).and.ibpm>0) then
            pbpm=>monitors(ibpm)%p
           call gmSetTextSetting(wnbpm ,pbpm%mag%name)
           call gmSetValueSetting(wibpm ,ibpm)
           call e_gm_draw_magnet(PLOT_ring_Graphics4,pbpm,colour=gOrange)
           call gFlushGraphics
          call gmSetValueSetting(Form_plot_linear_Value98 ,monitors(ibpm)%kind )
  
          else
           ibpm=size(monitors)
            pbpm=>monitors(ibpm)%p
           call gmSetTextSetting(wnbpm ,pbpm%mag%name)
           call gmSetValueSetting(wibpm ,ibpm)
           call e_gm_draw_magnet(PLOT_ring_Graphics4,pbpm,colour=gOrange)
           call gFlushGraphics
          call gmSetValueSetting(Form_plot_linear_Value98 ,monitors(ibpm)%kind )
         endif
  
         endif
      else if (callid == (Form_plot_linear_nturn)) then
         m_turn= gmEnqValueSetting(Form_plot_linear_nturn )
         if(m_turn>size(monitors(1)%r,2)) then
         m_turn=size(monitors(1)%r,2)
         call gmSetValueSetting(Form_plot_linear_nturn,m_turn)
         endif
      else if (callid == (Form_plot_linear_Button102)) then
         call draw_bpm_data
         if(plane_bpm==1) then
         plane_bpm=3
         i1=2
         else
         plane_bpm=1
         i1=1
         endif
  
          call gmSetValueSetting(Form_plot_linear_Value97,i1 )
      else if (callid == (Form_plot_linear_Button102+GIDENT)) then
         call draw_bpm_data
      else if (callid == (Form_plot_linear_Button103)) then
         call set_size_bpm
      else if (callid == (Form_plot_linear_Value97)) then
         plane_bpm= 2*gmEnqValueSetting(Form_plot_linear_Value97 )-1
      else if (callid == (Form_plot_linear_Button104)) then
         call alex_average_r_jparc
  
      else if (callid == (Form_plot_linear_Button105)) then
         call gmClearGraphicsFrame(plot(9))
      else if (callid == (Form_plot_linear_Button106)) then
         If(ibpm/=0) then
          monitors(ibpm)%kind=1
         call gmSetValueSetting(Form_plot_linear_Value98 ,1 )
         endif
      else if (callid == (Form_plot_linear_Button107)) then
         If(ibpm/=0) then
          monitors(ibpm)%kind=2
          call gmSetValueSetting(Form_plot_linear_Value98 ,2 )
         endif
      else if (callid == (Form_plot_linear_Button108)) then
         If(ibpm/=0) then
          monitors(ibpm)%kind=3
          call gmSetValueSetting(Form_plot_linear_Value98 ,3 )
         endif
  
      else if (callid == (Form_plot_linear_Button109)) then
         If(ibpm/=0) then
          do i=1,size(monitors)
          monitors(i)%kind=-iabs(monitors(i)%kind)
          enddo
          call gmSetValueSetting(Form_plot_linear_Value98 ,monitors(ibpm)%kind )
         endif
      else if (callid == (Form_plot_linear_Button110)) then
         if(ibpm/=0) then
          MY_FIX=0.D0
          my_fix(5)=my_delta
          call alex_mom_real_monitors(my_ering,ibpm,jn,my_fix,my_estate,my_order,m_turn)
          write(6,*) jn
           call alex_print_xf(ibpm,"bpm_phase_plot.dat")
         endif
      else if (callid == (Form_plot_linear_Button111)) then
         call draw_bpm_pq
      else if (callid == (Form_plot_linear_Button112)) then
         call scale_bpm
      else if (callid == (Form_plot_linear_Button113)) then
         If(ibpm/=0) then
  
         fib=>my_ering%start
         do i=1,my_ering%n
         if(associated(fib,monitors(ibpm)%p)) exit
         fib=>fib%next
         enddo
         name=fib%mag%name
         call context(name)
  
         if(i/=0)my_start=i
  
           call move_to(my_ering,fib,my_start)
           call gmSetTextSetting(Form_states_name ,fib%mag%name)
           call gmSetValueSetting(Form_States_start,my_start )
  
         call fill_t(fib,th)
  
         my_start_t= gmEnqValueSetting( th(4) )
  
         around_fix=.true.
         call gmSetToggleSwitch(Form_States_fixpoint ,gon )
         if(around_fix.and.(i/=0)) then
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-7,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions
         endif
         call warn_t(fib,Form_States_line)
  
         dis_pos=my_start
           call gmSetValueSetting(Form_lat_val,fib%i%beta(n_lat))
           call gmSetValueSetting(Form_lat_s,fib%i%s)
            call gmSetValueSetting(Form_lat_phase1,fib%i%beta(1))
            call gmSetValueSetting(Form_lat_phase2,fib%i%beta(2))
            call gmSetValueSetting(Form_lat_phase3,fib%i%beta(3))
           call gmSetTextSetting(Form_lat_mag_name ,fib%mag%name)
                    call gmSetToggleSwitch(Form_dyn_use_a ,goff )
                    ray%do_a_for_dyn=.false.
         call gmActivateGraphicsFrame(plot(4))
         call e_gm_draw_magnet(plot(4),fib,colour=gred)
         call gFlushGraphics
         call write_girders(fib)
  
  
         endif
  
      else if (callid == (Form_plot_linear_Button115)) then
         call alex_mom_monitors(ibpm,m_turn)
      else if (callid == (Form_plot_linear_Button116)) then
         call alex_apply_A_on_data(ibpm)
  
      else if (callid == (Form_plot_linear_with_c)) then
         if(with_c==1) then
         with_c=0
         call gmSetWidgetProp(Form_plot_linear_with_c,gmLabel="Use Coupling")
         else
         with_c=1
         call gmSetWidgetProp(Form_plot_linear_with_c,gmLabel="Ignore Coupling")
         endif
  
      else if (callid == (Form_plot_linear_all)) then
         do i=1,size(monitors)
          if(monitors(i)%full) then
            monitors(i)%kind=3
          else
            monitors(i)%kind=-3
          endif
         enddo
      else if (callid == (Form_plot_linear_compute_all)) then
         do i=1,size(monitors)
         if(monitors(i)%full) then
          MY_FIX=0.D0
          my_fix(5)=my_delta
          call alex_mom_real_monitors(my_ering,i,jn,my_fix,my_estate,my_order,m_turn)
          write(6,*) jn
          call alex_mom_monitors(i,m_turn)
          ! call alex_print_xf(ibpm,"bpm_phase_plot.dat")
         endif
         enddo
      else if (callid == (Form_plot_linear_comp_lat)) then
         call compute_lattice_function_monitors
         call gmSetWidgetStatus(M_plotbetax, GSELECTABLE)
         call gmSetWidgetStatus(m_scale_plot, GSELECTABLE)
         call gmSetWidgetStatus(m_scalebetax, GSELECTABLE)
         call gmSetWidgetStatus(M_auto_scale, GSELECTABLE)
         call gmSetWidgetStatus(M_select, GSELECTABLE)
  
  
      else if (callid == (Form_plot_linear_Button124)) then
  
         if(allocated(monitors)) then
          ibpm= ibpm+1
  
         if(ibpm>size(monitors)) ibpm=1
  
            pbpm=>monitors(ibpm)%p
           call gmSetTextSetting(wnbpm ,pbpm%mag%name)
           call gmSetValueSetting(wibpm ,ibpm)
           call e_gm_draw_magnet(PLOT_ring_Graphics4,pbpm,colour=gOrange)
           call gFlushGraphics
          call gmSetValueSetting(Form_plot_linear_Value98 ,monitors(ibpm)%kind )
  
         endif
  
         call gmClearGraphicsFrame(plot(9))
         call set_size_bpm
         call draw_bpm_data
         if(plane_bpm==1) then
         plane_bpm=3
         i1=2
         else
         plane_bpm=1
         i1=1
         endif
         call draw_bpm_data
  
          call gmSetValueSetting(Form_plot_linear_Value97,i1)
      else if (callid == (Form_plot_linear_Button124+GIDENT)) then
         call draw_bpm_data
      else if (callid == (Form_plot_linear_ignore)) then
         if(monitors(ibpm)%full) then
         monitors(ibpm)%full=.false.
         call gmSetWidgetProp(Form_plot_linear_ignore,gmLabel="Use bpm")
         else
         monitors(ibpm)%full=.true.
         call gmSetWidgetProp(Form_plot_linear_ignore,gmLabel="Ignore bpm")
         endif
  
      else if (callid == (Form_plot_linear_list)) then
         call kanalnummer(i1,"list_bpm.txt")
         do i=1,size(monitors)
          write(i1,*) i,monitors(i)%full
         enddo
         close(i1)
  
      else if (callid == (Form_plot_linear_Button127)) then
         call kanalnummer(i1,"list_bpm.txt")
         do i=1,size(monitors)
          read(i1,*) i2,monitors(i)%full
         enddo
         close(i1)
  
      else if (callid == (Form30_twiss_alx)) then
         al_(1)=gmEnqValueSetting(Form30_twiss_alx)
  
      else if (callid == (Form30_twiss_betax)) then
         bet_(1)=gmEnqValueSetting(Form30_twiss_betax)
  
      else if (callid == (Form30_twiss_aly)) then
         al_(2)=gmEnqValueSetting(Form30_twiss_aly)
  
      else if (callid == (Form30_twiss_betay)) then
         bet_(2)=gmEnqValueSetting(Form30_twiss_betay)
  
      else if (callid == (Form30_twiss_Button89)) then
         ait=ai0
  
  
      else if (callid == (Form30_twiss_make_ait)) then
         ait(1,1)=sqrt(bet_(1))
         ait(2,2)=1.d0/sqrt(bet_(1))
         ait(3,3)=sqrt(bet_(2))
         ait(4,4)=1.d0/sqrt(bet_(2))
         ait(2,1)=-al_(1)/sqrt(bet_(1))
         ait(4,3)=-al_(2)/sqrt(bet_(2))
         ait(5,5)=1.d0
         ait(6,6)=1.d0
  
      else if (callid == (Form30_twiss_qf)) then
         call gmEnqTextSetting(Form30_twiss_qf ,nametg(1) )
         call context(nametg(1))
  
      else if (callid == (Form30_twiss_qd)) then
         call gmEnqTextSetting(Form_States_name ,nametg(2) )
         call context(nametg(2))
  
      else if (callid == (Form30_twiss_qx)) then
         targt(1)=gmEnqValueSetting(Form30_twiss_qx)
  
      else if (callid == (Form30_twiss_qy)) then
         targt(2)=gmEnqValueSetting(Form30_twiss_qy)
  
      else if (callid == (Form30_twiss_fit_tunes)) then
         call lattice_fit_TUNE_gmap_auto(my_ering,my_estate,epsfr,targt,nametg)
  
      else if (callid == (Form30_twiss_epsfr)) then
         epsfr=gmEnqValueSetting(Form30_twiss_epsfr)
  
      else if (callid == (Form_resonance)) then
         this_plot=10
      else if (callid == (Form_resonance+GIDENT)) then
         this_plot=10
      else if (callid == (Form_resonance_plot_phi_i)) then
         this_plot=10
      else if (callid == (Form_resonance_plot_phi_i+GIDENT)) then
         this_plot=10
  
             IF(gEnqKeyState_et(ichar("B"))) THEN
               call gmColourControl(red, green, blue)
               call gDefineRGB(gbackground,red, green, blue)
               call gmClearGraphicsFrame(plot(this_plot))
             elseif(gEnqKeyState_et(ichar("1"))) then
              write(6,*) " 1st boundary "
                   limit_temp%xmin=actlist%x
                   limit_temp%ymin=actlist%y
                   write(6,*) actlist%x,actlist%y,(actlist%x**2+actlist%y**2)
            elseif(gEnqKeyState_et(ichar("2"))) then
              write(6,*) " 2nd boundary "
                   limit_temp%xmax=actlist%x
                   limit_temp%ymax=actlist%y
  
                   write(6,*) limit_temp
                   call e_g_order(limit_temp,limit_temp)
                   write(6,*) limit_temp
         !   call  gmSetGraphicsFrameBounds(plot(this_plot), limit_temp)
          plot_size(this_plot)%xmin = limit_temp%xmin ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%xmax = limit_temp%xmax ! /sig_plot(2*this_plot-1)
          plot_size(this_plot)%ymin = limit_temp%ymin ! /sig_plot(2*this_plot)
          plot_size(this_plot)%ymax = limit_temp%ymax ! /sig_plot(2*this_plot)
         ! plot_size(this_plot)%xmin= plot_size_sig(this_plot)%xmin *sig_plot(2*this_plot-1)
         ! plot_size(this_plot)%xmax= plot_size_sig(this_plot)%xmax *sig_plot(2*this_plot-1)
         ! plot_size(this_plot)%ymin= plot_size_sig(this_plot)%ymin *sig_plot(2*this_plot)
         ! plot_size(this_plot)%ymax= plot_size_sig(this_plot)%ymax *sig_plot(2*this_plot)
         call set_coordinate_fields
         call gmSetGraphicsFrameBounds(plot(this_plot),plot_size(this_plot))
               call gmClearGraphicsFrame(plot(this_plot))
  
             elseif(gEnqKeyState_et(ichar("M"))) then
              call track_special_one_d(  actlist%x,actlist%y,n_turns,ray%colour)
  
             else
  
            ! write(6,*)  actlist%x,actlist%y
  
            if(.not.gEnqKeyState_et(ichar("C"))) then
                I_0=actlist%y/j_scale
                psi_0=actlist%x
             endif
               call gmSetValueSetting( Form_resonance_i0, I_0*j_scale)
               call gmSetValueSetting( Form_resonance_psi0, psi_0 )
  
            call plot_sum_res(ray%colour,n_turns)
  
             endif
  
  
      else if (callid == (Form_resonance_mr1)) then
         mur(1) = gmEnqValueSetting(form_resonance_mr1 )
      else if (callid == (Form_resonance_mr2)) then
         mur(2) = gmEnqValueSetting(form_resonance_mr2 )
      else if (callid == (Form_resonance_dmr1)) then
         dmur(1) = gmEnqValueSetting(form_resonance_dmr1 )
      else if (callid == (Form_resonance_dmr2)) then
         dmur(2) = gmEnqValueSetting(form_resonance_dmr2 )
      else if (callid == (Form_resonance_dmr3)) then
         dmur(3) = gmEnqValueSetting(form_resonance_dmr3 )
      else if (callid == (Form_resonance_a)) then
           A_str = gmEnqValueSetting(form_resonance_a )
      else if (callid == (Form_resonance_phi)) then
          phi_str = gmEnqValueSetting(form_resonance_phi )
      else if (callid == (Form_resonance_compute)) then
         this_plot=10
         call sum_res
  
         !if(gmEnqToggleSwitch_et(state(9))) then
          around_fix=.true.
          MY_FIX=0.D0
          my_fix(5)=my_delta
         call FIND_ORBIT_x(my_eRING,my_FIX,my_eSTATE,1.d-5,turns=turns,fibre1=my_start)
               call gmSetValueSetting(Form_orbit_x  ,my_fix(1) )
               call gmSetValueSetting(Form_orbit_px ,my_fix(2) )
               call gmSetValueSetting(Form_orbit_y  ,my_fix(3) )
               call gmSetValueSetting(Form_orbit_py ,my_fix(4) )
               call gmSetValueSetting(Form_orbit_pt ,my_fix(5) )
               call gmSetValueSetting(Form_orbit_t  ,my_fix(6) )
         call check_ptc_exceptions(fix=my_fix)
         !else
          !around_fix=.false.
         !endif
         call gmSetToggleSwitch(state(9),gon)
  
      else if (callid == (Form_resonance_compute+GIDENT)) then
         this_plot=10
      else if (callid == (Form_resonance_sum)) then
          sum_res_val = gmEnqValueSetting(form_resonance_sum )
      else if (callid == (Form_resonance_J)) then
         j_perp = gmEnqValueSetting(form_resonance_j )/j_scale
  
      else if (callid == (Form_resonance_jsol1)) then
         jsol(1) = gmEnqValueSetting(form_resonance_jsol1 )/j_scale
  
      else if (callid == (Form_resonance_jsol2)) then
         jsol(2) = gmEnqValueSetting(form_resonance_jsol2 )/j_scale
  
      else if (callid == (Form_resonance_Button129)) then
         call compute_jsol
      else if (callid == (Form_resonance_dt)) then
         dt_sum_res = gmEnqValueSetting(form_resonance_dt )
      else if (callid == (Form_resonance_psi0)) then
         psi_0 = gmEnqValueSetting(form_resonance_psi0 )
      else if (callid == (Form_resonance_i0)) then
         i_0 = gmEnqValueSetting(form_resonance_i0 )/j_scale
  
  
      else if (callid == (Form_resonance_Button130)) then
         call use_previous_ray
      else if (callid == (Form_resonance_Button131)) then
         call send_to_use_previous_ray
      end if
  
  
! End of Action Loop
if (callid == (track_accel)) then
do i1=1,k_cav_plot
 call track_accel_dejan_beam(my_ering)
icode=icode+1
enddo
 Write(6,*) " calls ",icode
if(icode==n_turns) then
call gmDefineTimerCallback(0,track_accel)
 call gmDefineTimerCallback(0,track_plot)
 iplot=0
 ifile=0
endif
nturn_dejan=0
if(k_cav_plot>1) then
nturn_dejan=mod(icode,k_cav_plot)
endif
  
if(nturn_dejan==0) then
ifile=ifile+1
 Write(6,*) " files",ifile
write(plot1(13:120),'(1x,i8,a4)')ifile,'.jpg'
call context(plot1)
i=frasav(plot(1),plot1)
  
write(plot2(13:120),'(1x,i8,a4)')ifile,'.jpg'
call context(plot2)
i=frasav(plot(2),plot2)
  
 write(plot3(13:120),'(1x,i8,a4)')ifile,'.jpg'
 call context(plot3)
i=frasav(plot(3),plot3)
endif
  
endif !  if(mod(icode,k_cav_plot)
  
if (callid == (track_plot)) then
if(kplot==0) then
 kplots=1
 kplote=3
else
 kplots=kplot
 kplote=kplot
endif
do kplotl=kplots,kplote  !
call gmActivateGraphicsFrame(plot(kplotl))
iplot=iplot+ncode
  
write(plotmovie(kplotl)(13:120),'(1x,i6,a4)')iplot,'.jpg'
call context(plotmovie(kplotl))
  
INQUIRE (FILE = plotmovie(kplotl), EXIST = exists)
  
if(exists) then
 call gGetImageFile(GJPEGFILE,plotmovie(kplotl),1,0,0,xgrid,ygrid,48000,pixbuf)
 call gDrawPixelArea(1,1,xgrid,ygrid,1,1,xgrid,ygrid,pixbuf)
 call gflushgraphics()
else
 call gmDefineTimerCallback(0,track_plot)
 iplot=0
  
endif
enddo  ! end kplotl=kplots,kplote
endif
  
if (callid == (siamese_flash)) then
if(nsiamese<0) then
 call gmDefineTimerCallback(0,siamese_flash)
 call e_gm_draw_magnet_siamese(plot(this_plot),fib,siamese_colour,nsiamese)
else
 call e_gm_draw_magnet_siamese(plot(this_plot),fib,siamese_colour,nsiamese)
nsiamese=nsiamese+1
endif
endif
  
if (callid == (girder_flash)) then
if(ngirder<0) then
 call gmDefineTimerCallback(0,girder_flash)
 call e_gm_draw_magnet_girder(plot(this_plot),fib,girder_colour,ngirder)
else
 call e_gm_draw_magnet_girder(plot(this_plot),fib,girder_colour,ngirder)
ngirder=ngirder+1
endif
!girder_flash=100003,time_girder=300,ngirder=0
  
endif
  
if (callid == (track_lin)) then
do i1=1,n_turns
!fanglei  call track_my_beam(my_ering,my_e_Beam,my_estate,my_start)
ilin=ilin+1
enddo
 do i=1,n_nd
   if(around_fix) then
   call egm_scale_draw_e_beam(my_e_beam,i,my_FIX)
  else
  call egm_scale_draw_e_beam(my_e_beam,i)
 endif
 enddo
endif
  
  
  
      if(callid == -1) exit
   end do
  
! Program termination code
9999 CONTINUE
if(allocated(zturns)) deallocate(zturns)
  
! Close device MENU and GINO, and end program
 if(first_gino==100) then 
   call gmCloseMenu
   call gCloseDevice
   call gCloseGino
 endif 
 first_gino=0 
  
 end subroutine Call_gino 
! Additional User Routines/Functions                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                               
 subroutine open_gino_graphics                                                                                                                                                                                                                                 
 implicit none                                                                                                                                                                                                                                                 
 character*48 command_gino                                                                                                                                                                                                                                     
 command_gino="OPENGINO"                                                                                                                                                                                                                                       
  call call_gino(command_gino)                                                                                                                                                                                                                                 
  command_gino="MINI"                                                                                                                                                                                                                                          
  call call_gino(command_gino)                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                               
 end subroutine open_gino_graphics                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                               
  subroutine close_gino_graphics                                                                                                                                                                                                                               
 implicit none                                                                                                                                                                                                                                                 
 character*48 command_gino                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                               
   command_gino="CLOSEGINO"                                                                                                                                                                                                                                    
  call call_gino(command_gino)                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                               
 end subroutine close_gino_graphics                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                               
!subroutine fit_inj(ti8,lhc2)                                                                                                                                                                                                                                  
!use run_madx                                                                                                                                                                                                                                                  
!use pointer_lattice   ! added                                                                                                                                                                                                                                 
!implicit none                                                                                                                                                                                                                                                 
!type(layout), target :: ti8,lhc2                                                                                                                                                                                                                              
!type(pol_block) pol_ti8(6)                                                                                                                                                                                                                                    
!integer n,POS,NT,NEQ,SCRATCHFILE,NPT,I,J,KK                                                                                                                                                                                                                   
!type(real_8) y(6)                                                                                                                                                                                                                                             
!LOGICAL(LP) US                                                                                                                                                                                                                                                
!TYPE(FIBRE),POINTER :: P                                                                                                                                                                                                                                      
!TYPE(TAYLOR) T,EQ(4)                                                                                                                                                                                                                                          
!TYPE(GMAP) G                                                                                                                                                                                                                                                  
!REAL(DP) X(6)                                                                                                                                                                                                                                                 
!                                                                                                                                                                                                                                                              
!NPT=6                                                                                                                                                                                                                                                         
!NEQ=4                                                                                                                                                                                                                                                         
!NT=NEQ+NPT                                                                                                                                                                                                                                                    
!                                                                                                                                                                                                                                                              
!pol_ti8(1)=0                                                                                                                                                                                                                                                  
!pol_ti8(1)%name="MSIB"                                                                                                                                                                                                                                        
!pol_ti8(1)%n_name=4                                                                                                                                                                                                                                           
!pol_ti8(1)%ibn(1)=1                                                                                                                                                                                                                                           
!                                                                                                                                                                                                                                                              
!pol_ti8(2)=0                                                                                                                                                                                                                                                  
!pol_ti8(2)%name="MSIA"                                                                                                                                                                                                                                        
!pol_ti8(2)%n_name=4                                                                                                                                                                                                                                           
!pol_ti8(2)%ibn(1)=2                                                                                                                                                                                                                                           
!                                                                                                                                                                                                                                                              
!DO N=1,2                                                                                                                                                                                                                                                      
! Ti8=pol_ti8(N)                                                                                                                                                                                                                                               
!ENDDO                                                                                                                                                                                                                                                         
!                                                                                                                                                                                                                                                              
!pol_ti8(3)=0                                                                                                                                                                                                                                                  
!pol_ti8(3)%name="MKI.D5R8.B2"                                                                                                                                                                                                                                 
!pol_ti8(3)%iAn(1)=3                                                                                                                                                                                                                                           
!pol_ti8(4)=0                                                                                                                                                                                                                                                  
!pol_ti8(4)%name="MKI.C5R8.B2"                                                                                                                                                                                                                                 
!pol_ti8(4)%iAn(1)=4                                                                                                                                                                                                                                           
!pol_ti8(5)=0                                                                                                                                                                                                                                                  
!pol_ti8(5)%name="MKI.B5R8.B2"                                                                                                                                                                                                                                 
!pol_ti8(5)%iAn(1)=5                                                                                                                                                                                                                                           
!pol_ti8(6)=0                                                                                                                                                                                                                                                  
!pol_ti8(6)%name="MKI.A5R8.B2"                                                                                                                                                                                                                                 
!pol_ti8(6)%iAn(1)=6                                                                                                                                                                                                                                           
!                                                                                                                                                                                                                                                              
!!MCBYH.4R8.B2                                                                                                                                                                                                                                                 
!                                                                                                                                                                                                                                                              
!DO N=3,6                                                                                                                                                                                                                                                      
! LHC2=pol_ti8(N)                                                                                                                                                                                                                                              
!ENDDO                                                                                                                                                                                                                                                         
!                                                                                                                                                                                                                                                              
!DO KK=1,4                                                                                                                                                                                                                                                     
!                                                                                                                                                                                                                                                              
!call init(1,6)                                                                                                                                                                                                                                                
!call alloc(y)                                                                                                                                                                                                                                                 
!CALL ALLOC(EQ,4)                                                                                                                                                                                                                                              
!WRITE(6,*) c_%npara                                                                                                                                                                                                                                           
!PAUSE 444                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                               
!CALL MOVE_TO(LHC2,P,"MB.C24L8.B2",POS)                                                                                                                                                                                                                        
!!WRITE(6,*) P%MAG%NAME,POS                                                                                                                                                                                                                                    
!P=>P%NEXT                                                                                                                                                                                                                                                     
!US=.FALSE.                                                                                                                                                                                                                                                    
!call track_probe_x(Y,+default,fibre1=ti8%start,fibre2=p)                                                                                                                                                                                                      
!  us=.not.check_stable                                                                                                                                                                                                                                        
!!call track(ti8,y,1,ti8%n+1,+default)                                                                                                                                                                                                                         
!                                                                                                                                                                                                                                                              
!call print(y,6)                                                                                                                                                                                                                                               
!EQ(1)=Y(1)                                                                                                                                                                                                                                                    
!EQ(2)=Y(2)                                                                                                                                                                                                                                                    
!EQ(3)=Y(3)                                                                                                                                                                                                                                                    
!EQ(4)=Y(4)                                                                                                                                                                                                                                                    
!                                                                                                                                                                                                                                                              
!    call kanalnummer(SCRATCHFILE)                                                                                                                                                                                                                             
!    OPEN(UNIT=SCRATCHFILE,FILE='EQUATION.TXT')                                                                                                                                                                                                                
!    rewind scratchfile                                                                                                                                                                                                                                        
!                                                                                                                                                                                                                                                              
!    do i=1,neq                                                                                                                                                                                                                                                
!       eq(i)=eq(i)<=c_%npara_fpp                                                                                                                                                                                                                              
!    enddo                                                                                                                                                                                                                                                     
!    do i=1,neq                                                                                                                                                                                                                                                
!       call daprint(eq(i),scratchfile)                                                                                                                                                                                                                        
!    enddo                                                                                                                                                                                                                                                     
!    close(SCRATCHFILE)                                                                                                                                                                                                                                        
!                                                                                                                                                                                                                                                              
!                                                                                                                                                                                                                                                              
!                                                                                                                                                                                                                                                              
!    CALL INIT(1,nt)                                                                                                                                                                                                                                           
!    call alloc(g,nt)                                                                                                                                                                                                                                          
!    call kanalnummer(SCRATCHFILE)                                                                                                                                                                                                                             
!    OPEN(UNIT=SCRATCHFILE,FILE='EQUATION.TXT')                                                                                                                                                                                                                
!    rewind scratchfile                                                                                                                                                                                                                                        
!    do i=npT+1,nt                                                                                                                                                                                                                                             
!       call read(g%v(i),scratchfile)                                                                                                                                                                                                                          
!    enddo                                                                                                                                                                                                                                                     
!    close(SCRATCHFILE)                                                                                                                                                                                                                                        
!                                                                                                                                                                                                                                                              
!    call alloc(t)                                                                                                                                                                                                                                             
!    do i=1,npT                                                                                                                                                                                                                                                
!       g%v(i)=one.mono.i                                                                                                                                                                                                                                      
!       do j=npT+1,nt                                                                                                                                                                                                                                          
!          t=g%v(j).d.i                                                                                                                                                                                                                                        
!          g%v(i)=g%v(i)+(one.mono.j)*t                                                                                                                                                                                                                        
!       enddo                                                                                                                                                                                                                                                  
!    enddo                                                                                                                                                                                                                                                     
!    CALL KILL(t)                                                                                                                                                                                                                                              
!                                                                                                                                                                                                                                                              
!    g=g.oo.(-1)                                                                                                                                                                                                                                               
!    tpsafit(1:nt)=g                                                                                                                                                                                                                                           
!                                                                                                                                                                                                                                                              
!    SET_TPSAFIT=.true.                                                                                                                                                                                                                                        
!                                                                                                                                                                                                                                                              
!DO N=1,2                                                                                                                                                                                                                                                      
! Ti8=pol_ti8(N)                                                                                                                                                                                                                                               
!ENDDO                                                                                                                                                                                                                                                         
!                                                                                                                                                                                                                                                              
!DO N=3,6                                                                                                                                                                                                                                                      
! LHC2=pol_ti8(N)                                                                                                                                                                                                                                              
!ENDDO                                                                                                                                                                                                                                                         
!    SET_TPSAFIT=.false.                                                                                                                                                                                                                                       
!                                                                                                                                                                                                                                                              
!    CALL ELP_TO_EL(TI8)                                                                                                                                                                                                                                       
!    CALL ELP_TO_EL(LHC2)                                                                                                                                                                                                                                      
!                                                                                                                                                                                                                                                              
!ENDDO                                                                                                                                                                                                                                                         
!                                                                                                                                                                                                                                                              
!CALL KILL_PARA(TI8)                                                                                                                                                                                                                                           
!CALL KILL_PARA(LHC2)                                                                                                                                                                                                                                          
!                                                                                                                                                                                                                                                              
!                                                                                                                                                                                                                                                              
!end subroutine fit_inj                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                               
